Help with writing loop for multiple input files

Hi, I am having trouble writing a loop that reads multiple .raw files. The input files are 375x223x91 data sets. I have tried to write a for loop for this but have been unsuccessful. The details are:
vol=read_raw8b('Z:\520\ 1.raw'); %import data
Then executes code...
read_raw8b is a function:
function block=read_raw8b(filename)
fid=fopen([filename],'rb');
block=fread(fid,'*uint8','ieee-le');
fclose(fid);
I would like to write a loop that after executing the code for data set '1.raw' it then loops and imports '2.raw', then executes the same code and so on for 52 data sets.
Also I have a variable in the code called 'map' and I would also greatly appreciate if you could let me know how I can increment this so that when it reads '1.raw' and executes the code it stores variable 'map_1' and when it reads '2.raw' and executes the code it stores as 'map_2' and so on.
Thank you

댓글 수: 4

Adam
Adam 2016년 5월 13일
Why would you want variables map_1 and map_2 instead of just a single variable array containing map(1) and map(2)?
ajk1
ajk1 2016년 5월 13일
Hi Adam, thank you for the question. I wanted to have separate variables because later I would like to be able to plot map_1, map_2, etc... as they will be 375x223 size. Also be able to plot them on-top of each other. If I will be able to do that using a single variable array map(1) and map(2) then great, but would need help doing that.
Stephen23
Stephen23 2016년 5월 13일
편집: Stephen23 2016년 5월 13일
@ajk1: changing the names of variables in a loop is a really bad idea. While it is possible, it will be slow, buggy, obfuscated code. As Adam already implied, your best option is to simply use one variable and some indexing. Try using a cell array, it is really simple.
Read this thread and all of its links to know more about why you should not dynamically access variable names:
Read those links carefully! Notice how many expert MATLAB users have clearly written "do NOT create variable names in a loop". Also notice how many beginners keep inventing this idea!
Many beginners believe that defining or accessing variables in a loop would be a great idea. It isn't. It is a bad way to write code.
Just use one variable and some indexing.
ajk1
ajk1 2016년 5월 13일
Will do, thank you for your help.

댓글을 달려면 로그인하십시오.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 12일
for k=1:n
file=sprintf('Z:\520\%d.raw',k)
vol=read_raw8b(file)
end

댓글 수: 4

ajk1
ajk1 2016년 5월 13일
편집: ajk1 2016년 5월 13일
Thank you for your help. I edited my question shortly after posting it (adding an additional query below). I would be very grateful if I could get your help with the 'map' variable (as described above). Thank you.
ajk1
ajk1 2016년 5월 13일
편집: Azzi Abdelmalek 2016년 5월 13일
I tried running the code:
for k=1:52
file=sprintf('Z:\520\%d.raw',k)
vol=read_raw8b(file)
% Code to be executed
end
However I received these errors:
Error using fread
Invalid file identifier. Use fopen to generate a valid file
identifier.
Error in read_raw8b (line 4)
block=fread(fid,'*uint8','ieee-le');
Error in count_cracks_3D (line 3)
vol=read_raw8b(file);
Please let me know if I am running it incorrectly, I have tried altering the read_raw8b function, using fopen due to the error using fread, however this also did not work. Thanks.
John BG
John BG 2016년 5월 13일
편집: John BG 2016년 5월 13일
I completely agree with Azzi,
don't listen to that chachara of 'don't create variables in loops' without showing you consistent evidence.
If it works and the delay is acceptable, you pack it and you sell it, next one please, if you understand what I mean.
So, on the loop, just one correction, with Azzi's permission, instead of sprintf try
L1='Z:\520\';
for k=1:52
file=strcat(L1,char(k+48),'.raw')
vol=read_raw8b(file)
end
and because you said you have up to 52 files, and you may want to have them listed in alphabetic order, you may want to consider naming them 01.raw 02.raw ..
so, you may want to insert in Azzi's loop the following:
L1='Z:\520\';
for k=1:52
if k<10
file=strcat(L1,'0',char(k+48),'.raw');
else
file=strcat(L1,char(floor(k/10)+48),char(rem(k,10)+48),'.raw');
end
vol=read_raw8b(file)
end
John
ajk1
ajk1 2016년 5월 14일
편집: ajk1 2016년 5월 14일
Perfect, the code is working now. Thanks to all that have helped and advised.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 5월 12일

편집:

2016년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by