Hello,
I am trying to convert a .CSV file to .mat in matlab. I have attached the .CSV file. In the file the columns D,E,F,G which is marked has values like 8,2,7,13 which should belong to a field name called labelfacegrid. Here's the code i have written so far.
clc;
close all;
clear all;
T = readtable('E:/Metadata_create.csv','ReadVariableNames',false);
frameIndex = T{:,1};
labelDotXCam = T{:,2};
labelDotYCam = T{:,3};
labelFaceGrid0 = T{:,4};
labelFaceGrid1 = T{:,5};
labelFaceGrid2 = T{:,6};
labelFaceGrid3 = T{:,7};
labelRecNum = T{:,8};
labelTest = T{:,9};
labelTrain = T{:,10};
labelVal = T{:,11};
save('E:/mymat.mat','frameIndex', 'labelDotXCam', 'labelDotYCam', 'labelFaceGrid0','labelFaceGrid1','labelFaceGrid2','labelFaceGrid3', 'labelRecNum', 'labelTest', 'labelTrain', 'labelVal')
As seen above i have put variable name as labelFaceGrid0,labelFaceGrid1,labelFaceGrid2,labelFaceGrid3 for now but in reality i want it to be a single variable with 4 different values like as in csv file the columns D,E,F,G. What code changes should i make to accomodate those four values for a single variable or a field name. I am stuck. Thank you.
csv.PNG

4 件のコメント

Walter Roberson
Walter Roberson 2019 年 9 月 25 日
Madan Kumar Y
Madan Kumar Y 2019 年 9 月 25 日
Hey Walter,
Thank you will give a try.
Madan Kumar Y
Madan Kumar Y 2019 年 9 月 25 日
Hey Walter,
looks like mergevars isn't supported in the version of matlab i have that is R2014a. Any other alternatives??.
Thanks,
Madan
Ankit
Ankit 2019 年 9 月 25 日
編集済み: Ankit 2019 年 9 月 25 日
Hello Madan,
first recommendations is to avoid using clear all;
You can save your variables in structure format as mentioned below. Is it that what you are looking for?
clc;
close all;
T = readtable('E:/Metadata_create.csv','ReadVariableNames',false);
frameIndex = T{:,1};
labelDotXCam = T{:,2};
labelDotYCam = T{:,3};
labelFaceGrid.a = T{:,4};
labelFaceGrid.b = T{:,5};
labelFaceGrid.c = T{:,6};
labelFaceGrid.d = T{:,7};
labelRecNum = T{:,8};
labelTest = T{:,9};
labelTrain = T{:,10};
labelVal = T{:,11};
save('E:/mymat.mat','frameIndex', 'labelDotXCam', 'labelDotYCam', 'labelFaceGrid', 'labelRecNum', 'labelTest', 'labelTrain', 'labelVal')
Thanks
Ankit

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2019 年 9 月 25 日

1 投票

Not exactly sure what you're asking, is it simply?
labelFaceGrid = T{:, 4:7}; %extract column 4 to 7 as a matrix

6 件のコメント

Madan Kumar Y
Madan Kumar Y 2019 年 9 月 25 日
Hey Guillaume,
yeah something like that, but it's throwing error in save, like saying variable 'labelFaceGrid' not found.
Thanks,
Madan
Ankit
Ankit 2019 年 9 月 25 日
have you change the variable name that you are saving from "labelFaceGrid0" ... to labelFaceGrid ?
Guillaume
Guillaume 2019 年 9 月 25 日
Show us the exact code you're using now, then. Obviously if you've created a labelFaceGrid variable as above, save is not going to complain that it doesn't exist, so you must have done something different.
Madan Kumar Y
Madan Kumar Y 2019 年 9 月 25 日
Hey Ankith,
Ya i have done that. whatever variable i am assigning, the same i am using it in save.
Thanks,
Madan
Ankit
Ankit 2019 年 9 月 25 日
編集済み: Ankit 2019 年 9 月 25 日
@Guillaume he is saving the *.mat file as follows I just change the name from labelFaceGrid to labelFaceGrid1 (see below)
save('E/mymat.mat','frameIndex', 'labelDotXCam', 'labelDotYCam', 'labelFaceGrid1', 'labelRecNum', 'labelTest', 'labelTrain', 'labelVal')
Following error matlab throws:
Error using save
Variable 'labelFaceGrid1' not found.
as Mr. Guillaume mentioned could you please post your code and error please?
Madan Kumar Y
Madan Kumar Y 2019 年 9 月 25 日
Hey Guillaume,
Gracias, it worked. There was a spelling mistake, my bad :/ .
Hey ankit,
Thanks for the suggestions, it was helpful.
Thanks,
Madan

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by