Azzera filtri
Azzera filtri

How to create a string array and access the elements without throwing "COMMAND must be a string scalar or character vector" error?

2 visualizzazioni (ultimi 30 giorni)
Hi, I am trying to have a command that operates on a series of different strings (specifically, different file names). Normally I would use fullfile and something with an asterisk marker to get all the files I want from a folder, but in this case, each file has a very distinct name, and if I try to use my normal method I will end up with way too many files.
I tried to pass in the file names by using a strings array [dumps=strings(fname1,fname2,fname3,fname4,fname5)], which gave me the following 5x1 string array:
This array was then submitted to the following function:
function [] = plotSimBox(dumpFiles)
for i=1:length(dumpFiles)
The loadDumpFile function takes in a file name as an input, so that's what it's looking for. Even though each element of this array is a string, when I try to access them in this way, I receive the error "COMMAND must be a string scalar or character vector." Here's the complete error message:
Error using GBMD.loadDumpFile (line 634)
COMMAND must be a string scalar or character vector.
Error in plotSimBox (line 7)
I'm not sure if there's a better way to construct an array of strings or to access them?

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 6 Apr 2024
Spostato: Fangjun Jiang il 6 Apr 2024
This error might be particular to the GBMD.loadDumpFile(). Can you try either one of these
  1. Make a temp variable, such as tempVar=dumpFiles(i); and then GBMD.loadDumpFile(tempVar)
  2. try GBMD.loadDumpFile(char(dumpFiles(i)))
  2 Commenti
Fangjun Jiang
Fangjun Jiang il 7 Apr 2024
This reply was added and later deleted by the OP.
@Fangjun Jiang, your comment worked for me. I had tried a temp variable and I had tried using string string(dumpFiles(i)), but I hadn't tried char, which ended up working. Thanks!
Fangjun Jiang
Fangjun Jiang il 7 Apr 2024
Althouth this is a workaround, the original error message indicated that the code inside GBMD.loadDumpFile() is not robust, as tempVar=dumpFiles(i) is a string scalar.
Or indeed, GBMD.loadDumpFile() only accepts char array, and then whatever "COMMAND" was constructed based on that char array.

Accedi per commentare.

Più risposte (0)


Scopri di più su Characters and Strings in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by