Create Variables in loop
显示 更早的评论
I have a large dataset and need to load it into chunks and sort it into different files. So I start out with the first part of my raw data and sort it into binary User 1, User 2, User 3, ... files. For this purpose I created a user loop. I first use fopen, then load the current chunk of raw data, then run the user loop, then use fwrite, then load the next chunk till the data sorting is done and cloase all the user flies (fclose).
So far I have written out:
x = 1;
while x <= length(userlist_long_day)
if x == 1
username_1 = userlist_long_day(x,:);
filename_1 = [dir_B_E_PD_Day SLASH 'Output_Day' date_name, '_ID' username_1 '_data' fileending];
fid_1 = fopen(filename_1,'wb','ieee-be');
end
...
...
end
for all of my users. But it makes the code pretty dense and it is very annoying having to change every single User every time I want to change my saving format (I have at least 6 different files to open, write and close for each user). I wanted to change the variable names in a loop but saw that it is not advisable. Does someone know an alternative that I could use?
1 个评论
"But it makes the code pretty dense and it is very annoying having to change every single User every time I want to change my saving format..."
Yes, I would imagine it is.
"I wanted to change the variable names in a loop but saw that it is not advisable"
The MATLAB documentation specifically advises against doing this. As do all experienced MATLAB users.
"Does someone know an alternative that I could use?"
Yes! In one word: indexing. Indexing is neat, simple, easy to debug, and very efficient. Indexing is what you should use, either with one struct array, or two cell arrays and numeric array.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!