How to save variable after every trials so that it stores different values for each of the different trials without getting overwriiten values

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-1-16
编辑:Stephen23 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
ANKIT MAURYA 2023-1-17
Hi stephen,
Thank you for explaining first two questions clearly.
The third question---
"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.
I have attached the whole program initially when i asked the question.
I am attaching again here for your perusal

请先登录,再进行评论。

回答(2 个)

MFK
MFK 2023-1-16
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

ANKIT MAURYA
ANKIT MAURYA 2023-1-16
Hi MFK,
Can i directly copy and paste this (below) chunk of code in my program without adding/deleting anything ?
And if yes, then where should I paste, i mean at what location in my program?
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
  3 个评论
ANKIT MAURYA
ANKIT MAURYA 2023-1-16
filename=sprintf('Current_file_iter%d',i_trial);
save(filename,"Current_file");
end
It is already pasted before end, isn't it? or I am wrong to get what you are saying? Kindly clarify me. Thank You.
MFK
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by