Help with writing loop for multiple input files
Show older comments
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 Comments
Adam
on 13 May 2016
Why would you want variables map_1 and map_2 instead of just a single variable array containing map(1) and map(2)?
ajk1
on 13 May 2016
@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
on 13 May 2016
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!