Creating callable function with prompts

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

Carson Purnell
Carson Purnell am 16 Jun. 2022
You already got the speal on "don't try to dynamically name variables". However, this bit of code does not dynamically name variables or make use of dynamic variable names. It's dyanmically assigning values to nondynamic variables - which virtually every bit of useful code does. So i'm wondering if you are actually trying to do something else.
Can you elaborate on the problem/obstacle and the goal? because you might just want to use inputdlg to mass input a bunch of information manually (please don't do things manually when you could automate them though).
Zain
Zain am 16 Jun. 2022
Sure. So I am writing a script to perform loads analysis on rockets. Contractors give us data files with certain matrices. Often times, these matrices are given different names depending on the company who sends the data. We want the script to be a universal loads analysis script that doesn't have to hardcode the matrix names everytime, so we thought to use a prompt so the user can input a matrix name from the data file, and then assign that name to a variable that will stay constant throughout the rest of the script (reading the file to search for the matrix, etc). In order to compartmentalize the script a bit, I wanted to put this "naming" inside of a callable function, so that's the goal. Hope that helps
Carson Purnell
Carson Purnell am 16 Jun. 2022
It is not clear why you should care what the matrix name is, unless it's an actual matrix object stored in a .mat file. What is the format of the data files that makes it ever necessary to have the user manually figure out these dynamic bits of information? the script should never need to know that string, except in the case you want to generate new files from it.
Zain
Zain am 16 Jun. 2022
they're op4 files. I'm not too sure how exactly they are rea, but a previous employee wrote a function to read those files and extract matrices based on their names and all I have to do in my script is call the function to read it
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

0 Stimmen

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

Zain
Zain am 16 Jun. 2022
Hmm. The reason I want to try this is because I have a really long script where the variables might show up, but the actual name of them from the file they're reading may change depending on the case, so I thought condensing all this naming into a function would be beneficial. So there's no other way to name variables based on a user input inside a function? I would have have to just include all this naming at the beginning of the long script?
Zain
Zain am 16 Jun. 2022
And also thank you!
Stephen23
Stephen23 am 16 Jun. 2022
Bearbeitet: Stephen23 am 16 Jun. 2022
"but the actual name of them from the file they're reading may change depending on the case"
The most commonly-used function that can automatically generate variable names from file data is LOAD.
You can simply avoid the issue by LOADing into an output variable (which is a scalar structure):
S = load(..);
Your approach of magically naming variables should definitely be avoided.
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.

Produkte

Version

R2019b

Gefragt:

am 16 Jun. 2022

Kommentiert:

am 16 Jun. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by