- readtable: https://www.mathworks.com/help/matlab/ref/readtable.html
- assignin: https://www.mathworks.com/help/matlab/ref/assignin.html
how to create data from csv file whose name are in the first line of the csv
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
guillaume_from_fontainebleau
am 30 Okt. 2017
Beantwortet: Jaswanth
am 24 Okt. 2024 um 6:25
Dear all,
I have a csv file with records. This record is as such:
line 1, column 1: a string = 'time'
line 1, column 2: another string = 'current'
line 1, column 3: another string = 'voltage'
line 2, column 1: a numerical value, the first value of time. Example: 0.100
line 2, column 2: another numerical value, the first value of current. Example: 0.05
line 2, column 3: another numerical value, the first value of voltage. Example: 10
line 3, column 1: a numerical value, the second value of time. Example: 0.200
line 3, column 2: another numerical value, the second value of current. Example: 0.06
line 3, column 3: another numerical value, the second value of voltage. Example: 10
etc...
I would like to automatically create vectors like:
time(1) = 0.100
current(1) = 0.05
voltage(1) = 10
time(2) = 0.200
current(2) = 0.06
voltage(2) = 10
I already extracted the text et numerical values, using the function csvread, but I do not know how to automatically create vectors whose name is 'time', 'current', 'voltage'. Would you please help me? The aim is to be able to automatically generate vectors without having to care for what is recorded. What is recorded may change the csv will always have the same format with the first line with the name of recorded variable.
0 Kommentare
Antworten (1)
Jaswanth
am 24 Okt. 2024 um 6:25
Hi,
To convert data in a CSV file to MATLAB vectors, you can use the “readtable” function, which reads the file into atable structure[. This automatically uses the first row of the CSV as column headers, allowing to create vectors named after these headers.
Please refer to the following example code demonstrating the process described above:
% Read the CSV file into a table
dataTable = readtable('example.csv');
% Extract the column names from the table
variableNames = dataTable.Properties.VariableNames;
% Iterate over each column to create vectors
for i = 1:length(variableNames)
% Assign each column to a vector with the corresponding name
assignin('base', variableNames{i}, dataTable.(variableNames{i}));
end
You may refer to the following MathWorks documentation to know more about the functions mentioned above:
I hope the solution provided above is helpful.
0 Kommentare
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!