Effacer les filtres
Effacer les filtres

Brace indexing is not supported for this type of variable

3 vues (au cours des 30 derniers jours)
Kavinprasad M
Kavinprasad M le 19 Juin 2023
Modifié(e) : Stephen23 le 20 Juin 2023
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 commentaires
Kavinprasad M
Kavinprasad M le 20 Juin 2023
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 le 20 Juin 2023
Modifié(e) : Stephen23 le 20 Juin 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.

Connectez-vous pour commenter.

Réponses (2)

Ayush Kashyap
Ayush Kashyap le 19 Juin 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 le 19 Juin 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 commentaire
Kavinprasad M
Kavinprasad M le 20 Juin 2023
Modifié(e) : Kavinprasad M le 20 Juin 2023
Thankyou, this code is better but it still throws the same error for baseFileNames{k}
Attached complete application

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing 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!

Translated by