For loop only shows result of last loop
Show older comments
Hello, I am new to Matlab and I have a problem with for-loop function. I want to take out a certain data automatically from a *.dat file by using a for loop. This is the function that I used for reading my *.dat file.
%%file reading
[filename,pathname] = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on');
fid = fopen(fullfile(pathname,filename));
if fid == -1
disp('Error, check file name')
else
S= textscan(fid,'%f %f %s %s %f','Delimiter',' ');
end
And this is one of the sample data on the file
1518 0 Angle_PodHullInteractTable1 **@List@Definition@** 1
1518 1 Angle_PodHullInteractTable1 -180.0 1
1518 2 Angle_PodHullInteractTable1 -180.0 1
1518 3 Angle_PodHullInteractTable1 -180.0 1
1518 4 Angle_PodHullInteractTable1 -155.0 1
1518 5 Angle_PodHullInteractTable1 -155.0 1
1518 6 Angle_PodHullInteractTable1 -155.0 1
1518 7 Angle_PodHullInteractTable1 -145.0 1
1518 8 Angle_PodHullInteractTable1 -145.0 1
1518 9 Angle_PodHullInteractTable1 -145.0 1
1518 10 Angle_PodHullInteractTable1 -125.0 1
1518 11 Angle_PodHullInteractTable1 -125.0 1
1518 12 Angle_PodHullInteractTable1 -125.0 1
1518 13 Angle_PodHullInteractTable1 -115.0 1
1518 14 Angle_PodHullInteractTable1 -115.0 1
1518 15 Angle_PodHullInteractTable1 -115.0 1
1518 16 Angle_PodHullInteractTable1 180.0 1
1518 17 Angle_PodHullInteractTable1 180.0 1
1518 18 Angle_PodHullInteractTable1 180.0 1
I want to take out this data from the file by using for loop by using this function
Ergebnis{1,1} = (1:1:max(S{1,1}))';
for i = 1:1;
for ind = 1:19;
ind(:);
H(i,ind)=1 /(ind+i-1);
end
end
temp = strtok(char(S{1,3}(ind,1)));
if ~strcmp(char(S{1,4}(ind(1,1),1)),'n-a') && ~((temp(1,1)== 'C') && (isstrprop(temp(1,2),'digit'))) && ~strcmp(char(S{1,4}(ind(1,1),1)),'**ListDefinition**')
Ergebnis{1,2}(i,1) = S{1,3}(ind,1);
if ~isnan(str2double(S{1,4}(ind(1,1),1)));
Ergebnis{1,4}(i,1) = str2double(S{1,4}(ind(1,1),1));
Ergebnis{1,3}(i,1) = {''};
current_name = char(Ergebnis{1,2}(i,1));
current_val = Ergebnis{1,4}(i,1);
cmd = sprintf('%s = current_val;',current_name);
eval(cmd);
elseif isnan(str2double(S{1,4}(ind(1,1),1)));
Ergebnis{1,3}(i,1) = S{1,4}(ind(1,1),1);
Ergebnis{1,4}(i,1) = 0;
current_name = char(Ergebnis{1,2}(i,1));
current_val = char(Ergebnis{1,3}(i,1));
cmd = sprintf('%s = current_val;',current_name);
eval(cmd);
else disp('Fehler1')
end
elseif strcmp(char(S{1,4}(ind(1,1),1)),'**ListDefinition**')
Ergebnis{1,2}(i,1) = S{1,3}(ind,1);
Ergebnis{1,3}(i,1) = {''};
Ergebnis{1,4}(i,1) = 0;
current_name = char(Ergebnis{1,2}(i,1));
current_val = Ergebnis{1,4}(i,1);
cmd = sprintf('%s = current_val;',current_name);
eval(cmd);
elseif ((temp(1,1)== 'C') && (isstrprop(temp(1,2),'digit'))) || strcmp(char(S{1,4}(ind(1,1),1)),'n-a')
Ergebnis{1,2}(i,1) = {''};
Ergebnis{1,3}(i,1) = {''};
Ergebnis{1,4}(i,1) = 0;
else
disp('Fehler2')
end
But the result that shows only
Angle_PodHullInteractTable1= [180.0] %only the 19th rows in my file.
But the result that I looking for is
Angle_PodHullInteractTable1= [-180.0,-180.0,-180.0,-155.0,-155.0,-155.0,-145.0,-145.0,-145.0,-125.0,-125.0,-125.0,-115.0,-115.0,-115.0,180.0,180.0,180.0];
So, why it is only read the last iteration of my loop? and how to solve it this problem?
Thank you
1 Comment
Adam
on 21 May 2018
There are multiple things wrong here. Including, but not exhaustively:
- Your for loop on i is useless as you just loop from 1 to 1
- Your code that reads the file is not in a loop anyway therefore i would always be a scalar value even if your loop of i had sensible end value
- Don't dynamically name variables - you show the result of your code and I couldn't see a variable of that name declared anywhere so then I have to start looking at eval and then I just give up as all manner of unnecessary bugs can arrive when you use eval. Use structs or similar instead - https://uk.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Answers (1)
Guillaume
on 21 May 2018
I haven't fully tried to understand what your code is parsing but I can tell you it can be achieved much more easily. If you explain exactly what you want to parse and what the possible values are for the text (it looks like something could contain 'n-a') then we can give you simpler code.
why it is only read the last iteration of my loop?
None of your parsing is inside the loops. These are your whole loops:
for i = 1:1;
for ind = 1:19;
ind(:);
H(i,ind)=1 /(ind+i-1);
end
end
After these, i is 1 and ind is 19. Since you use ind as the row index, it's no wonder that after that you're only parsing row 19.
A few notes:
a)
for i = 1:1
is pointless. It's the same as
i = 1;
b) you don't normally put semicolon after a for statement
for ind = 1:19
instead of
for ind = 1:19;
c)
ind(:);
doesn't do anything.
d) You're dealing with vectors, hence use linear indexing. And of course, there's no need to index scalars. It's so much easier to read:
S{4}(ind)
than
S{1,4}(ind(1,1),1)
e) It looks like you want to generate variables named dynamically based on the content of your file. That is a very bad idea.
2 Comments
Rika Citra
on 24 May 2018
Edited: Rika Citra
on 24 May 2018
Whatever the end value for the loops are, it doesn't matter. The file reading is after the loops, hence you're only using the values of i and ind as they are after you exit the loop (i.e. last iteration value).
But as I said, it is very likely that whatever you want to achieve can be done in only a few lines of code without loops, so give us a better description of what it is you're trying to do. As Adam and I said, your code has many flaws. Another one I just spotted:
if ~isnan(str2double(S{1,4}(ind(1,1),1)))
%...
elseif isnan(str2double(S{1,4}(ind(1,1),1)))
%...
You already know hat isnan(str2double(S{1,4}(ind(1,1),1))) is true if you reach the elseif since it's the negation of the if test. Hence
if ~isnan(str2double(S{4}(ind)) %also simplified the indexing
%...
else
%...
avoid wasting time calculating something that is true and is much easier to read.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!