save does not recognize text scalar

The following code snippet fails unexpectedly:
clear;
myFilename = 'SavedData';
mustBeTextScalar(myFilename); % Throws exception if not true.
x = 3;
save(myFilename, x);
The variable myFilename passes the mustBeTextScalar as expected. However, the save command fails with the message
Error using save
Argument must be a text scalar.
Error in
saveTest (line 5)
save(myFilename, x);
^^^^^^^^^^^^^^^^^^^
QUESTION: Why does save not see myFilename as a text scalar, even though mustBeTextScalar passes it?
[MATLAB Version: 25.1.0.2943329 (R2025a)]

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 24 de Jul. de 2025
Editada: Fangjun Jiang el 24 de Jul. de 2025

0 votos

It is not about the variable name myFilename.
save(myFilename)
or
save(myFilename,'x')

4 comentarios

Richard
Richard el 24 de Jul. de 2025
Ah, so it's about the presentation of the name of the variable to be saved. That needs to be in quotes as well. Thank you!
Right.
save() permits non-constant expressions to be passed as the variable names. You can compute the names of the variables to save.
But more directly: the parsing of
save(myFileName,x)
has to follow the regular rules of MATLAB expressions. MATLAB interprets all function parameters before paying attention to the function being called. So myFileName is first looked up in context and the pointer to the text vector is prepared on the stack.Then, in this hypothetical context, the content of x would be looked up and the pointer to the numeric scalar would be prepared on the stack. Then save() would be called with those two items on the stack. save() would receive first the pointer to the text vector and then the pointer to the numeric scalar. But in doing so it would have lost the name of the numeric scalar and would only have its value.
This is why save() needs text vectors containing the names of variables to be saved, instead of needing the content of those variables.
Stephen23
Stephen23 el 25 de Jul. de 2025
"...But in doing so it would have lost the name of the numeric scalar and would only have its value. This is why save() needs text vectors containing the names of variables to be saved, instead of needing the content of those variables."
Yet TABLE manages to do this, so it turns out that functions do not "need" text inputs like this.
Well, table() uses inputname under the hood, and invents variable names for cases where expressions were passed. save() does not have the luxary of being able to invent variable names.
x = [1;2;3];
table(x, [4;5;6], +x)
ans = 3×3 table
x Var2 Var3 _ ____ ____ 1 4 1 2 5 2 3 6 3
... variable names are used where inputname() is able to figure them out, and otherwise variable names are invented for expressions.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Productos

Versión

R2025a

Preguntada:

el 24 de Jul. de 2025

Comentada:

el 25 de Jul. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by