How to assign new variables?

조회 수: 3 (최근 30일)
Devendra
Devendra 2024년 4월 13일
편집: Walter Roberson 2024년 4월 14일
I am using following lines of matlab code to assign new variables to print on plot.
if(dependent_vars{jj} == 'act_y_ha')
par = 'act\_y\_ha'
end
if(dependent_vars{jj} == 'act_per_ha')
par = 'act\_per\_ha'
end
This code is giving following error
par = 'act\_y\_ha'
Arrays have incompatible sizes for this operation.
Error in ds_reg (line 69)
if(dependent_vars{jj} == 'act_per_ha')
I request you to kindly suggest me to fix the error.
Devendra

채택된 답변

Konstantinos
Konstantinos 2024년 4월 13일
The error you're encountering, "Arrays have incompatible sizes for this operation," suggests that the comparison operation == between the string 'act_per_ha' and the cell array element dependent_vars{jj} is causing an issue due to incompatible sizes or types.
To fix this error, you can use the strcmp function to compare strings in MATLAB. Here's the corrected code:
if strcmp(dependent_vars{jj}, 'act_y_ha')
par = 'act\_y\_ha';
end
if strcmp(dependent_vars{jj}, 'act_per_ha')
par = 'act\_per\_ha';
end
  댓글 수: 6
Devendra
Devendra 2024년 4월 13일
Thank you very much for your help, Devendra
Star Strider
Star Strider 2024년 4월 13일
@Devendra — See: Answer

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

추가 답변 (1개)

Star Strider
Star Strider 2024년 4월 13일
Use the strrep function instead of the if block —
jj = 1;
dependent_vars{jj} = 'act_per_ha';
jj = 2;
dependent_vars{jj} = 'act_per_ha';
jj = 3;
dependent_vars{jj} = 'line with no underscores';
for jj = 1:numel(dependent_vars)
dependent_vars{jj} = strrep(dependent_vars{jj}, '_', '\_');
end
dependent_vars{:}
ans = 'act\_per\_ha'
ans = 'act\_per\_ha'
ans = 'line with no underscores'
.
  댓글 수: 2
Devendra
Devendra 2024년 4월 14일
Thank you for your help. Deva
Star Strider
Star Strider 2024년 4월 14일
My pleasure!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by