save does not recognize text scalar
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Richard
am 24 Jul. 2025
Kommentiert: Walter Roberson
am 25 Jul. 2025
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)]
0 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 24 Jul. 2025
Bearbeitet: Fangjun Jiang
am 24 Jul. 2025
It is not about the variable name myFilename.
save(myFilename)
or
save(myFilename,'x')
4 Kommentare
Stephen23
am 25 Jul. 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.
Walter Roberson
am 25 Jul. 2025
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)
... variable names are used where inputname() is able to figure them out, and otherwise variable names are invented for expressions.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!