필터 지우기
필터 지우기

Brace indexing is not supported for this type of variable

조회 수: 3 (최근 30일)
Kavinprasad M
Kavinprasad M 2023년 6월 19일
편집: Stephen23 2023년 6월 20일
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
Kavinprasad M
Kavinprasad M 2023년 6월 20일
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 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
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

Image Analyst
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
  댓글 수: 1
Kavinprasad M
Kavinprasad M 2023년 6월 20일
편집: Kavinprasad M 2023년 6월 20일
Thankyou, this code is better but it still throws the same error for baseFileNames{k}
Attached complete application

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by