Convert .csv file to .mat file

I have a csv file of 2 colunms and want to be saved as a .mat file(of desired name) with two variables of desired name(each variable of dimension 1xk)
please let me know how to write a matlab code for this.

4 comentarios

madhan ravi
madhan ravi el 10 de Feb. de 2019
Editada: madhan ravi el 10 de Feb. de 2019
T=readtable('mycsv.csv');
% ^^^^^^^^^------ your csv filename
p=T{:,1};
q=T{:,2};
save('mymat.mat','p','q')
% ^^^^^^^^^----- your resulting .mat filename
[comment moved to answer section]
Ainuddin Khan
Ainuddin Khan el 10 de Feb. de 2019
thanks friend.
Thank you very much.
Saidul islam Tanveer
Saidul islam Tanveer el 17 de Ag. de 2020
thanks
Elise Barbeau
Elise Barbeau el 2 de Feb. de 2021
What if I have several csv files in one folder and I want to convert them all to .mat?

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 10 de Feb. de 2019

6 votos

T=readtable('mycsv.csv');
% ^^^^^^^^^------ your csv filename
p=T{:,1};
q=T{:,2};
save('mymat.mat','p','q')
% ^^^^^^^^^----- your resulting .mat filename

Más respuestas (2)

Saidul islam Tanveer
Saidul islam Tanveer el 11 de Feb. de 2020

0 votos

i have 8855 rows and 1155 colums ,how cant i convert it to mat file

3 comentarios

Nahid Hasan
Nahid Hasan el 13 de Mzo. de 2020
Use import data option in matlab...see top bar
Saidul islam Tanveer
Saidul islam Tanveer el 14 de Mzo. de 2020
Thanks a lot.how can i covert times series data to array.I am trying to covert time series and date .it covert as 0.So,how can i solve this Nan file.
Imtiaz nabi
Imtiaz nabi el 4 de En. de 2022
can you give a bit more info on the time series data? maybe like showing your data rows etc so that I can guide you accordingly

Iniciar sesión para comentar.

Harsimran Singh
Harsimran Singh el 3 de Mayo de 2021

0 votos

use this link, it works perfectly for me:
Option Explicit
Sub FixCsvFiles()
Dim SelectFolder As String
Dim csvFiles As Variant
Dim csvWb As Workbook
Dim x As Integer
'browse for folder with csv files
On Error GoTo FixCsvFiles_Error
SelectFolder = GetFolder("c:\")
Application.ScreenUpdating = False
'Check user did not cancel folder selection
If SelectFolder = "" Then
MsgBox "No Folder Selected - Cannot continue", vbCritical
End
End If
SelectFolder = SelectFolder & "\"
csvFiles = Dir(SelectFolder & "*.csv")
Do While csvFiles <> ""
Set csvWb = Workbooks.Open(SelectFolder & csvFiles)
Rows("1:2").Delete
x = x + 1
csvWb.Close True
csvFiles = Dir
Loop
Application.ScreenUpdating = True
MsgBox "A total of " & CStr(x) & " files processed", vbInformation
On Error GoTo 0
Exit Sub
FixCsvFiles_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure FixCsvFiles of Module2"
End Sub
Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "BROWSE TO FOLDER LOCATION WITH CSV FILES"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Feb. de 2019

Comentada:

el 4 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by