How to store an array of headers in a MAT file?
15 views (last 30 days)
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.
Accepted Answer
Shivam
on 11 Sep 2024
Edited: Shivam
on 11 Sep 2024
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:
- Create a .MAT file and save it with new header names using save function and load them into the script.
- 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.
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
0 Comments
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!
0 Comments
See Also
Categories
Find more on Software Development Tools 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!