How to store an array of headers in a MAT file?
Show older comments
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 Comments
Stephen23
on 11 Sep 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
on 11 Sep 2024
Accepted Answer
More Answers (2)
Taylor
on 11 Sep 2024
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
Sameer
on 11 Sep 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!
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!