Need to read data from text format to matlab

1 次查看(过去 30 天)
function [von_s]=Read_ODB_outputs_node()
clear all
clc
close all
debug = 0;
if (debug)
debug_file = fopen('debug.txt', 'wt');
end
%while exist ('out.txt','file')==0
%pause(0.1)
%end
file = 'out.txt';
fidd = fopen(file);
i = 0;
pause(0.5)
numSteps = 0;
j=0;
von_S=zeros;
while ( ~feof(fidd) )
tline = fgetl(fidd);
i = i+1;
if (regexpi(tline, ' ***** POST1 NODAL STRESS LISTING *****')>0)
numSteps = numSteps + 1;
tline = fgetl(fidd);
i = i+1;
while(isempty(str2num(tline)))
tline = fgetl(fidd);
i = i+1;
end
while(~isempty(str2num(tline)))
j=j+1;
data_f = sscanf(tline, '%d %e %e %e %e %e ', [1,6]);
if (debug)
fprintf(debug_file, '%d\t%e\n', data_f(1), data_f(6));
end
node_number=data_f(1);
tline = fgetl(fidd);
i = i+1;
von_S(j)=data_f(6);
end
if (debug)
fprintf(debug_file, '\n');
end
end
end
if (debug)
fclose(debug_file);
end
fclose(fidd);
fclose all
error is
Error using str2num (line 35)
Input must be a character vector or string scalar.
Error in Read_ODB_outputs_node (line 29)
while(isempty(str2num(tline)))
  2 个评论
POTTA SATEESH
POTTA SATEESH 2021-4-17
results are coming but still showing errors.....
need to read node amd SEQV values and use them for another code....please help
Rik
Rik 2021-4-19
You should avoid str2num. Use str2double, textscan or sscanf instead.
I would also suggest not mixing the reading of a files with the processing of a file.
You can get my readfile function from the FEX. If you are using R2017a or later, you can also get it through the AddOn-manager. For R2020b and later you can also use readfile=@(varargin)cellstr(readlines(varargin{:})) (note that readlines has slightly different optional parameters).

请先登录,再进行评论。

回答(1 个)

Clayton Gotberg
Clayton Gotberg 2021-4-17
I was able to reproduce your error as below:
A = str2num('') % An empty character vector; Result: A = []
B = str2num([]) % An empty matrix; Result: the error you got
The documentation for fgetl says that, after the last line of the file is reached, the result of the function is -1. Because this is already a number, you will not be able to transform it with str2num.
I notice that you are using feof to stop execution at the end of the text file. The issue is that you are using fgetl several times before MATLAB evaluates the while statement again.
while ( ~feof(fidd) )
tline = fgetl(fidd); % First time it's used.
if %condition%
tline = fgetl(fidd); % Second time it's used.
while (isempty(str2num(tline)))
tline = fgetl(fidd); % Third time it's used.
i = i+1;
end
while(~isempty(str2num(tline)))
j=j+1;
%evaluation code
tline = fgetl(fidd); % Other third time it's used.
i = i+1;
von_S(j)=data_f(6);
end
% debug code
end
end
MATLAB only checks the condition of the while loop at the beginning of each new loop.
For example,
X = true;
while X==true
X = false;
X = true;
end
Will run forever because X is always true when MATLAB checks.
You either need to call one line each time the first while loop starts or you need to add stops in other parts of the code. For example, adding
if feof(fidd)
break
end
in each of your while loops should stop the program from executing after the end of the text file is reached.
However, you may prefer to reorganize your code as follows:
while ( ~feof(fidd) ) % The program runs as long as there is more file to read.
tline = fgetl(fidd); % Get the next line of the program.
if regexpi() % If the regexpi condition is met...
i=i+1;
if (~isempty(str2num(tline))) % If there is data in tline...
j=j+1;
%Code to evaluate the data in tline.
end
end
% debug code
end
The program reads every line of the file (I have a hunch your previous program was skipping some lines) and then evaluates each one according to the same conditions as your program.
Finally, note that isempty gives the same results whether the array is numeric or characters. Because of this, there is no need for str2num in the conditions.
  6 个评论
Rigo ZOO
Rigo ZOO 2022-2-25
Hi Dear Clayton,
I have similar issue to read a dat file, I tried to use your recommendations but unfortunately I still have the same error. Please can I share my script with you?
Rik
Rik 2022-2-25
@Rigo ZOO Please post your question as a separate thread. Before that, have a read here and here. Don't forget to attach your file (rename it to .txt to be able to upload it here).
Feel free to post a comment here with a link once you have posted your question.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by