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

39 views (last 30 days)
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:
"/fslgroup/compute/Al_Dataset/refilledDump/rstruct_S3_fcc_N1_1_1_Al_M99_201228.553.out"
"/fslgroup/compute/Al_Dataset/refilledDump/rstruct_S7_fcc_Nn29_n2_10_Al_M99_201228.151.out"
"/fslgroup/compute/Al_Dataset/refilledDump/rstruct_S23_fcc_Nn45_49_109_Al_M99_201228.1424.out"
"/fslgroup/compute/Al_Dataset/refilledDump/rstruct_S27b_fcc_N23_n19_1_Al_M99_210713.5462.out"
"/fslgroup/compute/Al_Dataset/refilledDump/rstruct_S197c_fcc_Nn109_n223_53_Al_M99_200714.31.out"
This array was then submitted to the following function:
function [] = plotSimBox(dumpFiles)
for i=1:length(dumpFiles)
[~,BBox,~,~]=GBMD.loadDumpFile(dumpFiles(i));
etc.
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)
[~,BBox,~,~]=GBMD.loadDumpFile(dumpFiles(i));
I'm not sure if there's a better way to construct an array of strings or to access them?

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 6 Apr 2024 at 21:35
Moved: Fangjun Jiang on 6 Apr 2024 at 22:53
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 Comments
Fangjun Jiang
Fangjun Jiang on 7 Apr 2024 at 14:12
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 on 7 Apr 2024 at 14:16
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!