Creating callable function with prompts

조회 수: 6 (최근 30일)
Zain
Zain 2022년 6월 16일
댓글: Zain 2022년 6월 16일
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
Stephen23
Stephen23 2022년 6월 16일
편집: Stephen23 2022년 6월 16일
"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 2022년 6월 16일
Makes sense. Thank you!

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2022년 6월 16일
편집: Adam Danz 2022년 6월 16일
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
Adam Danz
Adam Danz 2022년 6월 16일
> 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 2022년 6월 16일
I answered Carson's comment and I think that answers your first question as well. I'll try the table method. Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by