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
Manikanta Aditya 2025-4-8
Follow the below steps:
  1. Read Data from Excel: Use readtable or xlsread to read data from Excel into MATLAB.
  2. Extract Text and Data: Extract the text and data from the table or cell array.
  3. 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 CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by