Need to read data from text format to matlab
Afficher commentaires plus anciens
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 commentaires
POTTA SATEESH
le 17 Avr 2021
Rik
le 19 Avr 2021
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.
Réponses (1)
Clayton Gotberg
le 17 Avr 2021
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 commentaires
POTTA SATEESH
le 18 Avr 2021
Modifié(e) : POTTA SATEESH
le 18 Avr 2021
Clayton Gotberg
le 18 Avr 2021
Your new error is because you are asking for data_f before you define it. You only want to write over V_S when you get a non-empty line so why not move it into the second if statement?
POTTA SATEESH
le 18 Avr 2021
Modifié(e) : POTTA SATEESH
le 19 Avr 2021
Clayton Gotberg
le 19 Avr 2021
If you attach an example of the file you want the script to read, I can spend some time looking into it. Definitely look at @Rik's comment on this post as well. You will probably have a much easier time if you read the file into memory and then process it.
Rigo ZOO
le 25 Fév 2022
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?
Catégories
En savoir plus sur Entering Commands dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!