How to save variable after every trials so that it stores different values for each of the different trials without getting overwriiten values
이전 댓글 표시
In the below chunk of code, how can i save 'Current_file' variable after every trial, so that it stores 70 values for each of the 70 trials. There are total 70 number of trials and 161 images in Manipulable folder. Kindly refer to the full code attached here. Any help would be highly appreciated. Thank You.
folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
manipulable_image = dir(fullfile(folder, '/*.bmp'))
as = numel(manipulable_image);
imageseq = randperm(as);
for i=1:as
idx =imageseq(i);
img = imread(fullfile(folder,manipulable_image(idx).name));
Current_file = fullfile(folder,manipulable_image(idx).name);
end
댓글 수: 6
"how can i save 'Current_file' variable after every trial, so that it stores 70 values for each of the 70 trials."
The simple and efficient MATLAB approach is to use indexing, e.g. into a cell array:
But really this question is simpyl answered by the fact that you don't need to do anything: all of the filenames are already stored in the structure returned by DIR(), so why do you need to duplicate that data for no obvious benefit?
ANKIT MAURYA
2023년 1월 16일
편집: ANKIT MAURYA
2023년 1월 16일
"I think the structutre DIR() stores all the 161 images in the Manipulable_objects named folder , but the 'Current_file' variable should store only 70 random images out of 161 total images."
Nothing in your code seems to select 70 filenames.
In any case, you can simply perform that selection on the structure returned by DIR():
k = 70;
S = dir(..)
X = randperm(numel(S),k)
T = S(X)
Or indeed, make that exact same selection after the loop. Why make your code more complex?
ANKIT MAURYA
2023년 1월 16일
편집: ANKIT MAURYA
2023년 1월 16일
"In your code------ variable 'T' is storing 70 random images if the value of 'k' is 70 , right ?"
It stores the filenames in T, not the images.
If you make k==70 then RANDPERM will randomly select 70 of the images from that folder.
"out of these 70 images stored in T, which of the image is presented on which of the trial (for i_trial =1:70)."
I have no idea, becase nowhere in your question do you explain how a "trial" is represented or indicated or encoded.
ANKIT MAURYA
2023년 1월 17일
답변 (2개)
MFK
2023년 1월 16일
0 개 추천
You can use save Current_file variable after it completetly created with save(filename,variable) function.
And also you can use it with eval function to save with variable variables names like Current_file_iter1,Current_file_iter2,Current_file_iter3 ....
For this use
for i=1:i_trial
%%%%%%
%%%%%%
Current_file = fullfile(folder,manipulable_image(idx).name);
filename=sprintf('Current_file_iter%d',i_trial);
save(filename,"Current_file");
end
댓글 수: 1
Stephen23
2023년 1월 16일
"And also you can use it with eval function to save with variable variables names like Current_file_iter1,Current_file_iter2,Current_file_iter3 ...."
Best avoided: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
ANKIT MAURYA
2023년 1월 16일
0 개 추천
댓글 수: 3
MFK
2023년 1월 16일
filename=sprintf('Current_file_iter%d',i_trial); save(filename,"Current_file");
Pastr this before end try it for one iteration. It will probably work.
ANKIT MAURYA
2023년 1월 16일
MFK
2023년 1월 16일
folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
manipulable_image = dir(fullfile(folder, '/*.bmp'))
as = numel(manipulable_image);
imageseq = randperm(as);
for i=1:as
idx =imageseq(i);
img = imread(fullfile(folder,manipulable_image(idx).name));
Current_file = fullfile(folder,manipulable_image(idx).name);
filename=sprintf('Current_file_iter%d',i); %%% this is the code I suggest
save(filename,"Current_file"); %%% this is the code I suggest
end
You can use like that.
If you want to save in every iteration you can use like that. But if you want to save after all iterations completed. You can use a struct like;
folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
manipulable_image = dir(fullfile(folder, '/*.bmp'))
as = numel(manipulable_image);
imageseq = randperm(as);
for i=1:as
idx =imageseq(i);
img = imread(fullfile(folder,manipulable_image(idx).name));
Current_file{i,1} = fullfile(folder,manipulable_image(idx).name); %%% this is the code I suggest
end
save(Current_file,"Current_file"); %%% this is the code I suggest
카테고리
도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!