Dynamically Make a Variable Name (2024)

23 views (last 30 days)

Show older comments

Summer on 2 Apr 2024

  • Link

    Direct link to this question

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name

  • Link

    Direct link to this question

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name

Edited: Stephen23 on 3 Apr 2024

Accepted Answer: Paul

Hello,

I would like to make a variable name created from user input data, then make that variable a structure.

Right now I have a 1 x 2 structure called AllPlotInformation and I save the variable to a .mat file with a unique name (APlotData.mat, BPlotData.mat, etc). My issue is that if someone wants to load multiple files, the variable is called AllPlotInformation for every file so it gets overwritten. I would like a unique variable name for the struct so it will appear in the workspace when multiple files are loaded.

Here is an example:

letter= 'A';

color= 'blue;

tableVariablename= letter + color + "Table";

%this part I don't know how to make happen

AblueTable= struct('data1', data1, 'data2', data2_);

1 Comment

Show -1 older commentsHide -1 older comments

Stephen23 on 3 Apr 2024

Direct link to this comment

https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name#comment_3119101

  • Link

    Direct link to this comment

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name#comment_3119101

Edited: Stephen23 on 3 Apr 2024

"My issue is that if someone wants to load multiple files, the variable is called AllPlotInformation for every file so it gets overwritten"

Then you should be asking about that:

" I would like a unique variable name for the struct so it will appear in the workspace when multiple files are loaded."

Ugh, do NOT do that.

Unless you really want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:

https://www.mathworks.com/matlabcentral/discussions/tips/849901-tutorial-why-variables-should-not-be-named-dynamically-eval

You have well-designed data (every MAT file uses the same variable names) which allows you to easily write efficient and robust code to process that data. Do not ruin that good data design with dynamic variable names!

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Paul on 2 Apr 2024

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name#answer_1435341

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name#answer_1435341

Open in MATLAB Online

Hi Summer,

One option would be to use load with an output argument, which will put all of the variables the .mat file in a struct named by that output variable.

% create sme example data and files

AllPlotInformation = 1;

save file1 AllPlotInformation

AllPlotInformation = 2;

save file2 AllPlotInformation

s1 = load('file1')

s1 = struct with fields:

AllPlotInformation: 1

s2 = load('file2')

s2 = struct with fields:

AllPlotInformation: 2

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (1)

Stephen23 on 3 Apr 2024

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name#answer_1435511

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2102021-dynamically-make-a-variable-name#answer_1435511

Edited: Stephen23 on 3 Apr 2024

Open in MATLAB Online

This is MATLAB so the best solution is to use indexing, just as the MATLAB documentation shows:

https://www.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html

So far there is nothing in your question that prevents you from using neater, simpler, more efficient indexing.

For example:

P = 'absolute or relative path to where the files are saved';

S = dir(fullfile(P,'*.mat'));

for k = 1:numel(S)

F = fullfile(S(k).folder,S(k).name);

S(k).API = load(F).AllPlotInformation;

end

API = vertcat(S.API) % optional

All of the imported file data is stored in the structure S, which you can easily access using indexing. For example, the 2nd file:

S(2).name % filename

S(2).API % AllPlotInformation

Indexing is much better than your approach: neater, simpler, easier, more efficient, much more robust.

The optional line with VERTCAT concatenates all of the imported 1x2 structures into one Nx2 structure.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationMapping ToolboxData Import and Export

RadarMapping ToolboxData Import and Export

Find more on Data Import and Export in Help Center and File Exchange

Tags

  • variablename
  • variables
  • structures
  • evil
  • dynamic variable name
  • inefficient
  • bad data design

Products

  • MATLAB

Release

R2022a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Dynamically Make a Variable Name (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Dynamically Make a Variable Name (2024)

References

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5919

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.