Creating callable function with prompts

8 Ansichten (letzte 30 Tage)
Zain
Zain am 16 Jun. 2022
Kommentiert: Zain am 16 Jun. 2022
This is pretty simple, but I'm new to MATLAB. I created a bunch of user prompts that will define variable names based on user inputs. I am struggling to find a way to define a function that initiates all the prompts for the user to answer but in another script. For example, I have this:
path_prompt = 'What is the file path?';
file_path = input(path_prompt);
name_prompt = 'What is the file name?';
file_name = input(name_prompt)
% and so on for a couple more variables
How could I put this inside a function to be called in a much bigger script and still have the user interact with the prompts? Thank you in advance!!
  6 Kommentare
Stephen23
Stephen23 am 16 Jun. 2022
Bearbeitet: Stephen23 am 16 Jun. 2022
"Often times, these matrices are given different names depending on the company who sends the data."
So what? Your code's variable names should NEVER depend on this meta-information.
By all means store the filename as meta-data (in a variable) and make decisions based on it. But do NOT magically name variables based on what the user supplies, unless you really want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code which difficult to debug.
"We want the script to be a universal loads analysis script that doesn't have to hardcode the matrix names everytime"
Then don't "hardcode" the matrix names.
Load the filedata into one variable (using indexing) along with the corresponding filenames (meta-data). A structure array is perfect for this. Or a cell array or similar. Pick what suits your data and processing.
Problem avoided! Then you can start to write better** code.
** simpler, faster, neater, expandable, generalizable, much more efficient...
Zain
Zain am 16 Jun. 2022
Makes sense. Thank you!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Adam Danz
Adam Danz am 16 Jun. 2022
Bearbeitet: Adam Danz am 16 Jun. 2022
Include the "S" in argument 2 to return the entered text without evaluating it.
> created a bunch of user prompts that will define variable names
Yikes! It sounds like you're describing Dynamic Variable Naming which is strongly not recommended. See this informative, epic rant for details. If you're planning on using the user input strings as variable names, don't do it.
Instead, you could store them in an array or, better yet, a table, along with the variable values within another column of the table.
  5 Kommentare
Adam Danz
Adam Danz am 16 Jun. 2022
> So there's no other way to name variables based on a user input inside a function
It's not that there is no way to do it, it's that there is no good reason to do it while there are lots of reasons not to do it.
Let's make sure I understand your goal with this next question:
> I have a really long script where the variables might show up
How would the variables show up with you don't know what the variable names will be because the user names them?
> I would have have to just include all this naming at the beginning of the long script
You could name a bunch of variables (or 1 variable such as a table) at the beginning of the scipt and define those variables as the data input values.
If your data files contain 1) variable names such as headers in an excel file and 2) the corresponding data such as the column of values under the header line, then I recommend reading the data in as a table or constructing a table within MATLAB based on the data and the header names. Alternatively, you could read the data in as a matrix (assuming 2D data) and store the names in a separate variable (string array or cell-string) so that data(:,n) corresponds with variable name name(n).
Zain
Zain am 16 Jun. 2022
I answered Carson's comment and I think that answers your first question as well. I'll try the table method. Thank you!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by