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

13 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 11 Set 2024
"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 il 11 Set 2024
I plan on using a cell array, I just wanted to save the data in a mat file as well.

Accedi per commentare.

Risposta accettata

Shivam
Shivam il 11 Set 2024
Modificato: Shivam il 11 Set 2024
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 Commento
Norma
Norma il 11 Set 2024
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".

Accedi per commentare.

Più risposte (2)

Taylor
Taylor il 11 Set 2024
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 il 11 Set 2024
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!

Categorie

Scopri di più su Software Development Tools in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by