Brace indexing is not supported for this type of variable

Brace indexing not supported for this type of variable - error message, attached code, please support in resolving this issue
global new_files_all
[load_file, ~] = uigetfile('.mat','File load','MultiSelect', 'on');
new_files_all = load_file;
old_sname=extractBefore(load_file(1),'_');
for i=1:length(load_file)
sname = extractBefore(load_file{i},'_');
if i==1
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
old_sname = sname;
elseif (i>=2)
if snumber == 1 && strcmp(old_sname, sname)
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
elseif snumber >= 1 && strcmp(old_sname, sname)
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber;
old_sname = sname;
elseif snumber >= 1 && ~strcmp(old_sname, sname)
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber+1;
old_sname = sname;
end
end
new_files = {load_file{i}};

4 Kommentare

Stephen23
Stephen23 am 19 Jun. 2023
Bearbeitet: Stephen23 am 19 Jun. 2023
Why are you intentionally ignoring the filepath?
[F,P] = uigetfile('.mat','File load','MultiSelect', 'on');
F = cellstr(F); % <- this will probably fix that error
The class of UIGETFILE's output depends on how many files you select. Advice to simply use parenthesis indexing does not resolve that.
Inside of a function I recommend always calling load with an output argument. How do you know that one of your files doesn't contain a variable named load_file that replaces your list of files with a scalar number? As a simpler example, what do you think the contents of the variable pop is after running these two lines of code?
pop = 42;
load census.mat
No, it's not 42, because census.mat contained a variable named pop that overwrote that number with a vector.
pop
pop = 21×1
3.9000 5.3000 7.2000 9.6000 12.9000 17.1000 23.1000 31.4000 38.6000 50.2000
Yeah i can see the issue but my file will not have a variable named load_file since it's being directly exported from a data source i have externally which will only have certain input variable names
Stephen23
Stephen23 am 20 Jun. 2023
Bearbeitet: Stephen23 am 20 Jun. 2023
@Kavinprasad M: it is worth getting into the habit of writing more robust code, as Steven Lord recommends: the more you write it, the easier it becomes. And although you state that you are "certain" that the source only uses certain variable names, we get many many questions on this forum where the OP is "certain" of one thing or another... which turn out to be not true. The advice Steven Lord gave helps when the data is not as expected, as well as improving efficiency during its nominal/expected operation. Making code work is great... now you can upgrade your skills by controlling under what circumstances it will work and how it behaves when something unexpected happens. It will likely be more efficient to boot.
I second Steven Lord's recommendation: knowing how to write robust MATLAB code is definitely worth learning.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Ayush Kashyap
Ayush Kashyap am 19 Jun. 2023
Error message like this one: "Brace indexing not supported for this type of variable" appears mainly when curly braces "{}" are used for indexing instead of parantheses "()".
Looking at your code, I believe the issue is happeneing because of lines like this one:
sname = extractBefore(load_file{i},'_');
Try using parantheses as follows:
sname = extractBefore(load_file(i),'_');
Similarly, try to check your whole code once and replace all instances where you have used "{}" for indexing with "()".
Reference Link: Indexing in Matlab
Image Analyst
Image Analyst am 19 Jun. 2023
I think this is closer to what you want. It has many improvements. I'm not sure what you want with that sname stuff so there still may be errors.
global new_files_all
% Get a file specification.
folder = pwd; % Wherever you want.
filePattern = fullfile(folder, '*.mat');
% Ask user to select files.
[baseFileNames, folder] = uigetfile(filePattern,'File load','MultiSelect', 'on');
if folder == 0
fprintf('You clicked Cancel.\n');
return; % User clicked cancel.
end
numberOfFiles = numel(baseFileNames)
new_files_all = cell(numberOfFiles, 1);
old_sname=extractBefore(folder(1),'_'); % Not sure what this is for.
for k = 1 : numberOfFiles
thisFileName = fullfile(folder, baseFileNames{k});
fprintf('Processing %s (#%d of %d)\n', thisFileName, k, numberOfFiles);
sname = extractBefore(thisFileName,'_');
if k==1
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
old_sname = sname;
elseif (k>=2)
if snumber == 1 && strcmp(old_sname, sname)
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
elseif snumber >= 1 && strcmp(old_sname, sname)
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber;
old_sname = sname;
elseif snumber >= 1 && ~strcmp(old_sname, sname)
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber+1;
old_sname = sname;
end
end
new_files{k} = thisFileName;
end

1 Kommentar

Kavinprasad M
Kavinprasad M am 20 Jun. 2023
Bearbeitet: Kavinprasad M am 20 Jun. 2023
Thankyou, this code is better but it still throws the same error for baseFileNames{k}
Attached complete application

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Jun. 2023

Bearbeitet:

am 20 Jun. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by