How to store an array of headers in a MAT file?
조회 수: 11 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
Shivam
2024년 9월 11일
편집: Shivam
2024년 9월 11일
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.
추가 답변 (2개)
Taylor
2024년 9월 11일
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
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Software Development Tools에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!