Writing/Loading data that has runs 2 for loops

1 Ansicht (letzte 30 Tage)
University
University am 26 Nov. 2023
Beantwortet: Nipun am 15 Dez. 2023
Hi,
Lvals = 1:10;
xivals = 1:10;
%
tarray = 1:20;
nt = length(tarray);
%
lxi =length(Lvals);
nxi =length(xivals);
%
for il=1:lxi
pars.L= Lvals(il);
Lval=pars.L;
for ixi=1:nxi
pars.xi = xivals(ixi);
xival=pars.xi;
%
% writing activity to terminal
disp(['Run started with L = ' num2str(pars.L), ' xi = ' num2str(pars.xi)])
% creating file name with the activity value
filename = ['sol_' num2str(il), num2str(ixi)];
for it = 1:nt
u(ixi, il, :,:,it)
end
end
end
disp('Saving data...')
save(['C...' filename '.mat'], 'u');
It is expected that u is 5 dimensional, so that u is u(10, 10, :, :, nt). In my folder I have 100 entries for 10 different xi and 10 different L.
:, :, is the x and y array.
I tried:
disp('loading data')
filepath = 'C:folder';
datafilename = dir(fullfile(filepath,'*.mat'));
full_file_names = fullfile(filepath,{datafilename.name});
datafilename = natsortfiles(datafilename);
%
for n = 1:numel(datafilename)
F = fullfile(datafilename(n).folder,datafilename(n).name);
datafilename(n).data = load(F);
end
data = [datafilename.data];
This code is loading all the 100 files.
How do I load this data such that at the end I can extract u(xi, L, :, :, nt) such that xi=1:10 and L=1:10
Not xi=100 and L=100.

Antworten (1)

Nipun
Nipun am 15 Dez. 2023
Hi University,
I understand that you are trying to save multiple files and loading data from specific files using for loops, but the wrong file is being read by MATLAB. Based on the provided information, I can recommend few fixes that may solve this issue.
In the attached code, you are assigning the variable "filename" in each iteration, merely changing its value. I believe you intend to create an array of file names which will store the variable "u" value.
Consider the following modified code, with comments
Lvals = 1:10;
xivals = 1:10;
%
tarray = 1:20;
nt = length(tarray);
%
lxi =length(Lvals);
nxi =length(xivals);
%% Create an empty matrix of strings
filename = strings(lxi, nxi);
%
for il=1:lxi
pars.L= Lvals(il);
Lval=pars.L;
for ixi=1:nxi
pars.xi = xivals(ixi);
xival=pars.xi;
%
% writing activity to terminal
disp(['Run started with L = ' num2str(pars.L), ' xi = ' num2str(pars.xi)])
% creating file name with the activity value
%% Save the filename in the variable
filename(il,ixi) = "sol_"+num2str(il)+ num2str(ixi);
for it = 1:nt
u(ixi, il, :,:,it)
end
end
end
%% Save the specific slice of "u" in a .mat file
for i =1:lxi
for j=1:nxi
temp = u(i,j,:,:,nt);
save(filename(i,j)+".mat","temp",'-mat');
end
end
This code will generate 100 .mat files with specific variable values.
To read the specific slices of "u", just read the corresponding file name. For instance if you want to load "u(2,30,:,:,nt)"
load 'sol_23'
This command will load the intended variable in your workspace. Hope this helps
Regards,
Nipun

Kategorien

Mehr zu Downloads finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by