Clear Filters
Clear Filters

Load data and store in workspace in individual variables

2 views (last 30 days)
Hello, I'm trying to load data and that load data storing with different names in the workspace to use them later. So far I've done the following code:
for x = (0.75:0.75:19.5)
QTableS = 'Episodes%dQTableMatrixSetPoint%d.mat';
QTable_x = 'QTable_%d.mat';
A1 = 50;
A2 = x*100;
%%% Name data to load and data to store in workspace
str_QTable = sprintf(QTableS,A1,A2);
str_QTable_x = sprintf(QTableS,A2);
% load data and store in workspace
str_QTable_x = load(str_QTable);
str_QTable_x = str_QTable_x.QTable_Matrix;
end
So far it correctly loads the data from each of the store .mat files. But when storing the data to the workspace it overlaps each loop and I only have the last file loaded. What I want is to create a new variable with each loaded data in the workspace for each loaded file.
  1 Comment
Stephen23
Stephen23 on 4 Nov 2023
"What I want is to create a new variable with each loaded data in the workspace for each loaded file."
A much better approach is to use indexing, just as the MATLAB documentation shows:

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 3 Nov 2023
Can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!