error using system -- "Command argument must be either a nonmissing scalar string or a character row vector. "
22 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I donwloaded open source code that enables me to run Qspice from the command line and directly use its data in the MATLAB script. However, I get an error in the this section that I cannot understand:"Command argument must be either a nonmissing scalar string or a character row vector."
c = cmdsep; //this works in my version of MATLAB (2023b).
cmd_str = [
'path ',QspicePath,...
c,...
'QUX -Netlist "',Qpath.qsch,'"'
];
[status,Qpath.cir] = system(cmd_str)
it seems the syntax of the assembled path/file to run is incorrect, but I don't know what it needs to be.
The assembled cmd_str is:
cmd_str =
1×6 string array
"path " "C:\Program Files\QSPICE\" "&&" "QUX -Netlist "" "C:\Users\jorge.rive\OneDri…" """
The error I get when
system(cmd_str)
runs is:
Error using system
Command argument must be either a nonmissing scalar string or a character row vector.
Thank you in advance for the help.
Jorge
3 commentaires
Dyuman Joshi
le 10 Jan 2024
They both represent text data format, albeit are different data types.
For more information, see - https://in.mathworks.com/help/matlab/matlab_prog/represent-text-with-character-and-string-arrays.html
Réponse acceptée
Stephen23
le 10 Jan 2024
Modifié(e) : Stephen23
le 10 Jan 2024
"it seems the syntax of the assembled path/file to run is incorrect"
True, it is a 1x6 string array.
"but I don't know what it needs to be."
The error message tells you that it needs to be a scalar (i.e. 1x1) string or a character vector.
The basic problem is that you concatenated lots of text together without paying attention to what types they are. As you have found out, in general that won't work:
- one or more character vectors only will be concatenated into one character vector (this seems to be what you expected but did not confirm by checking your data).
- one or more string arrays with zero or more char vectors will be concatenated into an string array (with as many elements as the input string arrays have... plus one element for each char vector). This is what your data actually are.
Note that even just one string will completely change the concatenation behavior.
A simple and reliable approach is to use SPRINTF instead, e.g.:
cmd_str = sprintf('%s',...
'path ',QspicePath,...
c,...
'QUX -Netlist "',Qpath.qsch,'"');
It will accept any text type and return a character vector (just as that error message tells you).
4 commentaires
Voss
le 10 Jan 2024
Modifié(e) : Voss
le 10 Jan 2024
"I don't understand why the concatenated strings were still an array of strings instead of a "single" one."
@Jorge: What you were using (the square brackets, []) was array concatenation, which works the same for string arrays as it does for numeric arrays (and arrays of any class):
str1 = "hello"; % scalar string
str2 = "world"; % scalar string
[str1, str2] % 1x2 string array
n1 = 1; % scalar double-precision floating-point number (commonly called a "double")
n2 = 2; % scalar double
[n1, n2] % 1x2 double array
However, if you want to concatenate multiple strings into a single string, you need to perform string concatenation, which you can do using the plus (+) operator:
str1 + str2
str1 + " " + str2
or using a function for that purpose, e.g.:
strcat(str1,str2)
strcat(str1," ",str2)
join([str1,str2]," ")
"I don't understand what char actually does!"
The char function converts the input to a character array (not a string),
char(str1) % note the single quotes in the result -> character array, not a string
appending spaces as necessary and adding dimensions as necessary for non-scalar string array input:
str1 = "hell"; % using a shorter string to see the space-padding char does
str = [str1;str2] % 2x1 string input
char(str) % 2x5 char output
str = [str1,str2] % 1x2 string input
char(str) % 1x5x2 char output
str = cat(3,str1,str2)+reshape(1:12,3,[]) % 3x4x2 string input
char(str) % 3x7x4x2 char output
Plus de réponses (1)
Hassaan
le 10 Jan 2024
c = cmdsep; % This works in MATLAB 2023b
QspicePath = 'C:\Program Files\QSPICE\'; % Example path, replace with actual
Qpath.qsch = 'C:\Path\To\Your\Netlist.qsch'; % Replace with your actual netlist file path
% Construct the command string
cmd_str = strjoin([
'path ', QspicePath,
c,
'QUX -Netlist "', Qpath.qsch, '"'
], '');
% Execute the command
[status, Qpath.cir] = system(cmd_str);
% Check status and output
disp(status);
disp(Qpath.cir);
- strjoin is used with an empty delimiter '' to concatenate all the parts of the command into a single string.
- Ensure that QspicePath and Qpath.qsch contain the correct paths for your QSPICE installation and netlist file, respectively.
- The system command is then executed with cmd_str as a single concatenated string.
This should resolve the error and allow the command to be executed properly. However, make sure that the paths and command syntax are correct for your specific QSPICE setup and environment.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
2 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!