Is there a way to save matlab file with a name given by input?

7 次查看(过去 30 天)
HiHi! I'm creating an user interface with App Designer! My code runs and doesn't give problems, but I'd like to save my app with a name that is given by input from user. My input (that is found in a function of a button) is:
app.code_patient = inputdlg('Insert code of patient to save file with name:');
This works but I'd like that the name of matlab file is this input followed by '*.mat'. I tried to insert:
save app.code_patient.mat
but in this case matlab saves my results with the name 'app.code_patient.mat' and not with the name of given input.
  4 个评论
Stephen23
Stephen23 2018-4-1
编辑:Stephen23 2018-4-4
"but it doesn't work and I don't understand what is wrong!"
Tell us what happens: does the code run, or does it throw an error? What is the full error message? Does any file get saved? With the correct data, or incorrect data? Is the filename correct or incorrect?
We cannot see your monitor, and do not know what you are doing. If you do not explain exactly what you have tried, and exactly what "doesn't work" means, then we have no idea. There are a million ways that something might "not work", so you will have to tell us more useful information if you want help. Start by telling us:
  • what the value of app.code_patient is,
  • what you expected to happen,
  • what actually happened, (and how you checked this).
sc
sc 2018-4-3
The code gives an error using save because it must be a string scalar or character vector.
I try to explain better: I'd like that the results of code (of workspace) are saved in matlab file ('.mat') with the name that is given by user's input.
I created an 'inputdlg' that is called 'app.code_patient' that asks to user to insert the code of patient to save file with that name.
So I'd like that, after user pushed on button that is called 'Code of Patient', inserted this code and pushed on second button that is called 'Save Code', is possible to save matlab file with the code that is insert by user.
I tried to do this with save function but I don't understand what is wrong.
I'll attaches screenshots of my app: for example, in this case, I'd like that file name is 'patient_p', that is the input given by user.
I hope you can understand better.
Thank you so much!

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2018-3-30
Use function form, not command form, when you call save. See the last entry in the Description section on the save documentation page and the first entry in the Examples section on that page for examples of the difference.
  7 个评论
Stephen23
Stephen23 2018-4-4
编辑:Stephen23 2018-4-4
"In this case: foo is my app.file_name and 'x' is my '.mat', is it correct?"
No. And you can easily check this yourself by reading the save help. The listed syntaxes all show that the first argument must be the name of the file (including .mat if you wish), the second argument (and third, etc.) is the name of the variable that you wish to save to file (note: not the variable itself. Bad design, but that is how it is). If you wish to save all variables in the workspace then only the first argument is required.
The different syntaxes are shown in the save documentation, right at the top of the page. None of them show a file extension as the second input argument. Rather than guessing how to use MATLAB it is much easier just to read the documentation.
Also if you read the inputdlg help it states that the "return values are elements of a cell array of character vectors", whereas the save help states that the filename must be "specified as a character vector or string." Is that what your code does? To fix that bug you will need to get the char vector using cell indexing.
You should read the documentation for every function that you use.
sc
sc 2018-4-4
Ok, the problem was that I had not converted cell array to character vector. Now it works! Thank you for patience!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by