フィルターのクリア

Importing data: recognizing if a variable is present

4 ビュー (過去 30 日間)
Dylan Mecca
Dylan Mecca 2018 年 2 月 23 日
回答済み: Guillaume 2018 年 2 月 23 日
If I import data into matlab, how can I make it look for a specific variable? In the files attached, if z is present, then I'll do whatever calculations later in the code, and the same for a. I'm just a loss looking for a way to make matlab look for these variables. Is there a way to say to matlab, 'if a is present, then do some calculation' and vice versa for z?

採用された回答

SRT HellKitty
SRT HellKitty 2018 年 2 月 23 日
You can read about the Exist Command here.
For your example, you can do the following;
if exist('z','var') == 1
%Do z calculation
end
if exist('a','var') == 1
%Do a Calculations
end

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 2 月 23 日
If you use readtable to import your csv files, matlab will automatically recognise that there is a header and use the names in that header as variable names of your table (as long as these names make valid variable names). It is trivial to check if a variable is present in a table:
t = readtable('ex1.csv');
if ismember('z', t.Properties.VariableNames)
%z is a variable
plot3(t.x, t.y, t.z);
else
%z is not present
plot(t.x, t.y);
end

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by