try and catch prob
2 次查看(过去 30 天)
显示 更早的评论
Hi All, I am trying to read column headers in a file, however, sometimes the header varies by adding "_L" or "_R" to the name. I want to identify the row where the headers are using try/catch so if one set of variable names doesnt work then I use the modified one as below. Unfortunately, this is not working. I am expecting to get the second "emgvar" if the first one gives me a mismatch error. Help please.
emgvar = {'Spare_1';'Spare_2';'CH3_GME_';'CH4_GME_';'CH5_RF_L';'CH6_RF_R';'CH7_VL_L';'CH8_VL_R';'CH9_HB_L';'CH10_HB_';'CH11_TA_';'CH12_TA_';'CH13_GL_';'CH14_GL_';'CH15_SOL';'CH16_SOL'};
for n = 1:length(data)
if ~isempty(strfind(data{n},emgvar{1,1}))
E_rowNumber = n;
E_rowData = data{n};
splitRowData_E = regexp(E_rowData,' ','split');
try
for a = 1:length(emgvar (:,1))
E_loc (a,1) = find(ismember(splitRowData_E,emgvar {a,1}),1);
end
catch ME1
idSegLast = regexp (ME1.identifier, '(?<=:)\w+$','match');
if strcmp(idSegLast,'InvalidFid')
emgvar = {'Spare_1';'Spare_2';'CH3_GME_L';'CH4_GME_R';'CH5_RF_L';'CH6_RF_R';'CH7_VL_L';'CH8_VL_R';'CH9_HB_L';'CH10_HB_R';'CH11_TA_L';'CH12_TA_R';'CH13_GL_L';'CH14_GL_R';'CH15_SOL_L';'CH16_SOL_R'};
end
try
for a = 1:length(emgvar (:,1))
E_loc (a,1) = find(ismember(splitRowData_E,emgvar {a,1}),1);
end
catch ME2
fprintf('No data found');
end
end
end
end
0 个评论
回答(1 个)
Walter Roberson
2019-1-9
Why are you checking MATLAB:FileIO:InvalidFid ? That section of code is not reading a file.
If there are no elements matching that variable then the ismember() would return a vector that is completely false, and the find(,1) of that would return empty. You would then have an error from trying to assign emptiness to something. That would give you a MATLAB:subsassigndimmismatch error. Try/catch is a pretty heavy-handed way to do your checking.
I am a left uncertain in your code. Is each column individually subject to occur in base form or base_L form or base_R form ? Other than Spare_1 and Spare_2 ? Are CH3 through CH16 all required to appear, or can some be missing?
4 个评论
Walter Roberson
2019-1-9
You know you can strcmpn() ? Though now I am wondering why you are bothering to do this checking, when it sounds like you could simply look for lines that start with Spare_1 and then assume that the columns are the correct purpose and the correct order.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!