How to use cell value as a variable or How to convert cell to double?
9 次查看(过去 30 天)
显示 更早的评论
I have data in excel, I am reading that data in MATLAB. I want to use text in excel as a variable name to assign fixed set of data values which also in same excel. but I am facing the problem as the text from excel has class cell.
So, how can I use cell values (Time, Temperatue....etc) as a variable in MATLAB?
采纳的回答
Manikanta Aditya
2025-4-8
Follow the below steps:
- Read Data from Excel: Use readtable or xlsread to read data from Excel into MATLAB.
- Extract Text and Data: Extract the text and data from the table or cell array.
- Convert Text to Variable Names: Use eval or dynamic field names in a structure to assign data to variables.
% Step 1: Read data from Excel
data = readtable('yourfile.xlsx');
% Step 2: Extract text and data
variableNames = data.Properties.VariableNames;
dataValues = table2array(data);
% Step 3: Convert text to variable names and assign data
for i = 1:length(variableNames)
eval([variableNames{i} ' = dataValues(:, i);']);
end
% Example usage
disp(Time); % Assuming 'Time' is one of the column headers in your Excel file
disp(Temperature); % Assuming 'Temperature' is another column header
This code reads the data from an Excel file, extracts the column headers as variable names, and assigns the corresponding data to these variables. Make sure to replace 'yourfile.xlsx' with the actual name of your Excel file.
I hope this helps.
5 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!