Variable called angle (name conflict)

10 ビュー (過去 30 日間)
Jakob Sørensen
Jakob Sørensen 2012 年 9 月 11 日
I got a .mat file containing some data, some of which are single value variables. One of these are called 'angle' which conflicts with the Matlab function of the same name. This doesn't seem to be a problem if I just use it in the command prompt or a non-function m-file. But if i try to use it in a function, i get the following error:
Error in makeImage (line 49)
delta_theta = angle;
I can't change the variable in the .mat-file, since i have around 100 of them, and no it wasn't me who made the cock up :-). Any suggestions?

採用された回答

Teja Muppirala
Teja Muppirala 2012 年 9 月 11 日
I can think of two immediate ways to deal with this. But, as you've pointed out, it is of course best not to mix variable and function names.
1. You can load your MAT-file into a structure like this
function dosomething()
matdata = load('myfile.mat');
angle = matdata.angle
OR
2. You can declare "angle" as a variable right at the beginning, so that MATLAB knows it's not a function.
function dosomething()
angle = [];
load myfile
  2 件のコメント
Jakob Sørensen
Jakob Sørensen 2012 年 9 月 11 日
Thank you very much. Ironically, it was one of the university proffessors who made variable name.
Jan
Jan 2012 年 9 月 11 日
編集済み: Jan 2012 年 9 月 11 日
The name of the variable is intuitive. I suggest to use the struct instead, because for "matdata.angle" the source of the variable is clear and there are no conflicts. LOAD without catching the output causes unexpected bugs too often.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeR Language についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by