Saturday, October 18, 2014

Post #2. Choosing the file address to import datafiles

Someone, who works with stored data and needs to import new datafiles from different folders every time, needs to know how to import datafiles from specific folder address. In MATLAB, there is a function that help us with the task to looking for the desired folder. The function is uigetfile( ). This function opens the standard dialog for retrieving files. Specially for begginners, I'll indicate a code that introduces the use of this function.
The code starts with:

[file,dir] = uigetfile('.txt','Select file to import');


This line will make appear a open standard dialog box as showed below in the Fig 01.

Fig 01. Open standard dialog box. The title of the dialog box ('Select file to import') and the type of file ('.txt') to be imported were set in the command line above.

Once you chose the file to be imported, the function returns the value to the variables [file,dir]. The variable file will save the file name, and the variable dir will save the folder address. In the previous example, the variable file will be 'example.txt' , and the variable dir will be 'C:\Users\hp\Desktop\ '. Both information together will constitute the complete address of the file. The next line will concatenate the variables dir and file. 

FileToOpen =  [dir file];

The line above concatenated the both variables with [dir file], and the variable FileToOpen will receive the complete address of the file ('C:\Users\hp\Desktop\example.txt' ) to be imported.

The next lines will be used to evaluate if the variable FileToOpen is not valid. If it is ok, a new variable named Data will receive the information of the import command importdata( ) of the file addressed in FileToOpen. Finally the stored data will be ploted.

if FileToOpen ~= 0
    Data = importdata(FileToOpen);
    plot(Data(:,1),Data(:,2),'o')
else
end

This simple code can also be used to import different datafiles such as .csv and .mat. Any doubt contact me in givagosouza@gmail.com

  



No comments:

Post a Comment