Variable passed through function doesn't work

Hello all,
I have a function where I am passing three different behaviors (drug, before drug, after drug).
function defining_parsing(subj, path, data_type, behavior)
%% loading data
all_folders = dir(fullfile(path, subj, '/dir/')); % loading all the dir folders
% Define the regular expressions for each behavior and ROI
if behavior == 'drug'
expression_behavior = [data_type, '.*', '(s2|s4|s6|s3)', '.*'];
elseif behavior == 'before drug'
expression_behavior = [data_type, '.*', '(w3|w2|w4|w|WA)', '.*'];
elseif behavior == 'after drug'
expression_behavior = [data_type, '.*', '(EO)', '.*'];
end
end
When I pass the variable outside of the function for the above I get the following error:
Arrays have incompatible sizes for this operation.
Error in defining_parsing (line 20)
if behavior == 'drug'
When I pass the the variable for behavior within the function, I do not get the error:
function defining_parsing(subj, path, data_type, roi, behavior)
%%
behavior = 'drug'
%% loading data
all_folders = dir(fullfile(path, subj, '/dir/')); % loading all the dir folders
% Define the regular expressions for each behavior and ROI
if behavior == 'drug'
expression_behavior = [data_type, '.*', '(s2|s4|)', '.*'];
elseif behavior == 'before drug'
expression_behavior = [data_type, '.*', '(w3|w2|)', '.*'];
elseif behavior == 'after drug'
expression_behavior = [data_type, '.*', '(EO)', '.*'];
end
end
I have tried the following:
1) making sure that both the variable input to the function and the variable input with the function are the same (they are both char)
if ~ischar(behavior)
behavior = num2str(behavior);
end
2) making sure that there are no typos.
Do you have any suggestions? Thanks so much!

6 件のコメント

nines
nines 2023 年 1 月 26 日
I am calling the function:
behavior = 'drug'
data_type = 'intervetion'
for i = 1:length(subjs)
subj = subjs{i}
defining_parsing(subj, path, data_type, roi, behavior)
end
Torsten
Torsten 2023 年 1 月 26 日
function defining_parsing(subj, path, data_type, behavior)
function defining_parsing(subj, path, data_type, roi, behavior)
Do you see the difference in the list of input arguments ?
Jan
Jan 2023 年 1 月 26 日
@nines: A note: Do not call a variable "path", because this is an important built-in function. Shadowing it by a variable can cause very strange effects during debugging.
nines
nines 2023 年 1 月 26 日
Thank you!
Unfortunately it was not a function list problem -- that was a typo in my question/answer. I have switched to the switch function which solved the problem!
nines
nines 2023 年 1 月 26 日
final thing: how do i accept an answer? the accept button isn't coming up next to any of your names!
Fangjun Jiang
Fangjun Jiang 2023 年 1 月 26 日
編集済み: Fangjun Jiang 2023 年 1 月 26 日
I moved the comment by @Dyuman Joshi to an Answer. You can accept it now.

サインインしてコメントする。

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 1 月 26 日
移動済み: Fangjun Jiang 2023 年 1 月 26 日

0 投票

How are you calling the function?
Also, Use strcmp or isequal to compare strings
behavior = 'drug'
behavior = 'drug'
isequal(behavior,'drug')
ans = logical
1
strcmp(behavior,'drug')
ans = logical
1
You can also use switch here instead of if-else
switch behavior
case 'drug'
disp('1')
case 'before drug'
disp('2')
case 'after drug'
disp('3')
end
1

1 件のコメント

Fangjun Jiang
Fangjun Jiang 2023 年 1 月 26 日
編集済み: Fangjun Jiang 2023 年 1 月 26 日
This explains the root cause of the error message in the OP's question. When variable "behavior" takes the value of 'before drug' and it is compared to 'drug' in the If statement.
behavior='before drug';
behavior=='drug'
Arrays have incompatible sizes for this operation.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeRead, Write, and Modify Image についてさらに検索

タグ

質問済み:

2023 年 1 月 26 日

編集済み:

2023 年 1 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by