How to store an array of headers in a MAT file?

13 ビュー (過去 30 日間)
Norma
Norma 2024 年 9 月 11 日
コメント済み: Norma 2024 年 9 月 11 日
I'm trying to update any given table with a new header names imported from a .MAT file. I'm not sure how to approach this. Given the number of columns that the data file has, I want it to access a specific MAT file with new header names so that I can plot the preferred data. I don't know how to create a mat file within a script so it can save the all the headers names to it as VariableNames and not values.
  2 件のコメント
Stephen23
Stephen23 2024 年 9 月 11 日
"I'm not sure how to approach this"
"I don't know how to create a mat file within a script so it can save the all the headers names to it as VariableNames and not values."
So you are "not sure how to approach this", but you have already decided to reject storing your (meta-)data sensibly in an array and indead store your (meta-)data awkwardly in difficult-to-process variable names? Why make it harder for yourself?
Why not simply store the table column/variable names in a cell array (becase within the table object they are also accessible as a cell array), so you can trivially "update any given table" with them.
Norma
Norma 2024 年 9 月 11 日
I plan on using a cell array, I just wanted to save the data in a mat file as well.

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

採用された回答

Shivam
Shivam 2024 年 9 月 11 日
編集済み: Shivam 2024 年 9 月 11 日
Hi @Norma,
I understand that you want to update the headers of a table using new header names stored in a .MAT file. You can achieve this using the workaround provided below:
  1. Create a .MAT file and save it with new header names using save function and load them into the script.
  2. Update the exisiting table's header by setting new header names cell array to existingTable.Properties.VariableNames.
Here is how you can achieve it:
% Define new header names
newHeaderNames = {'Column1', 'Column2', 'Column3', 'Column4'};
% Save the header names to a .MAT file
save('newHeaders.mat', 'newHeaderNames');
% Load the header names from the .MAT file
loadedData = load('newHeaders.mat');
newHeaderNames = loadedData.newHeaderNames;
%
% Assuming you have previously created a table in the script
%
% Update the table headers with the new header names
existingTable.Properties.VariableNames = newHeaderNames;
I hope it helps.
  1 件のコメント
Norma
Norma 2024 年 9 月 11 日
Thank you! This helped me save it to a MAT file. It doesn't seem to like it when I update it to the timestable though. It only wants one item per "existingTable.Properties.VariableNames".

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

その他の回答 (2 件)

Taylor
Taylor 2024 年 9 月 11 日
I would recommend using switch/case. It might look something like this in practice
switch size(data, 2) % Switch based on the number of columns in the array "data"
case 3 % If there are three columns
loadedData = load('Headers3.mat');
headerNames = loadedData.headerNames;
dataTable = array2table(data);
dataTable.Properties.VariableNames = headerNames;
case 5 % If there are five columns
loadedData = load('Headers5.mat');
headerNames = loadedData.headerNames;
dataTable = array2table(data);
dataTable.Properties.VariableNames = headerNames;
end
To create a MAT file in a script, use the save command.

Sameer
Sameer 2024 年 9 月 11 日
Hi Norma
From my understanding you want to store an array of header names in a MAT file and then use these headers to update a table's variable names in MATLAB.
First, define your header names in a cell array and save them to a MAT file. Then, load these headers from the MAT file and apply them to update the variable names of your data table.
Below is an example MATLAB script:
% Define and save header names to a MAT file
headers = {'Time', 'Temperature', 'Pressure', 'Humidity'};
save('headers.mat', 'headers');
% Load headers from the MAT file
loadedData = load('headers.mat');
headers = loadedData.headers;
% Create a sample data table and update its headers
data = rand(10, 4); % Example data
T = array2table(data); % Convert data to a table
T.Properties.VariableNames = headers; % Update variable names
% Display the updated table
disp(T);
Hope this helps!

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by