Brace indexing is not supported for this type of variable
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 6월 20일
편집: Stephen23
2023년 6월 20일
@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.
답변 (2개)
Ayush Kashyap
2023년 6월 19일
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
댓글 수: 0
Image Analyst
2023년 6월 19일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!