フィルターのクリア

I need help plotting data from a file

1 回表示 (過去 30 日間)
Kenzie
Kenzie 2024 年 1 月 31 日
回答済み: Walter Roberson 2024 年 2 月 1 日
I am trying to write a code to plot the data from the mauna loa observatory, and after having previous trouble of matlab not being able to find my file, I am now getting an error on line 7 saying "Error using .
The VariableNames property must contain one name for each variable in the table."
here is my code:
% Load the data
filename = 'maunaloa_weekly.csv';
data = readtable(filename, 'ReadVariableNames', false, 'Delimiter', ',');
variableNames = {'Date', 'Values'};
% Assign variable names to the table
data.Properties.VariableNames = variableNames;
% Convert the dates to datetime format
dates = datetime(data.Date, 'Format', 'dd/MM/yyyy');
Values = data.Values;
% Create a time vector using datetime
time_vector = dates;
% Plot the data
figure;
subplot(2, 1, 1);
plot(dates, Values, 'LineWidth', 2);
title('Maunaloa Weekly Data');
xlabel('Time');
ylabel('Floating Point Values');
if i were to run this code now, there would be an error showing that they can find the file, but thats not the problem I am working on now.

採用された回答

Cris LaPierre
Cris LaPierre 2024 年 1 月 31 日
Your table must have more variable names than just those you are renaming. Try this.
data.Properties.VariableNames(1:2) = variableNames;
  1 件のコメント
Kenzie
Kenzie 2024 年 1 月 31 日
That worked, thank you!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 2 月 1 日
The table you are reading has some number of variables that is different than 2.
There are ways to specify the range that you want to read in, or which columns of the table you want to read. For example
opts = detectImportOptions(filename, 'ReadVariableNames', false, 'Delimiter', ',');
opts.SelectedVariableNames = {'Var1', 'Var3'};
data = readtable(filename, opts);
variableNames = {'Date', 'Values'};
% Assign variable names to the table
data.Properties.VariableNames = variableNames;
If you wanted to select the first and third columns.

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by