Differentiate b/w fscanf and load function in matlab [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my matlab program i am reading the data file using fscanf and writing the the code code to read all the values.That makes me write several steps.
How to use Load() function to overcome this ad make it simple.

So the way load works is to load variables from MATLAB binary/ascii files. In order to create said files you'll have to use the save function e.g.
octave:3> T = "Hello"
T = Hello
octave:4> save "-binary" "testfile" T
octave:5> clear
octave:6> T
error: 'T' undefined near line 1 column 1
octave:6> load "-binary" "testfile" T
octave:7> T
T = Hello
octave:8>
Sorry I used octave for the example but it's the same code either way. So if you know your going to be using the same data just save it in a MATLAB's binary format. It should save your self the time of having to use fscanf on it next time your playing around with the data.

Related

Dont understand the function of cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f')); at all [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
IND_STRT = 0;
ALL_STRT = IND_STRT:12:510;
cmd_data = zeros(length(ALL_STRT),1); %example: x=zeros(1,21) gives you a 1 by 21 matrix
for ii = 1:length(ALL_STRT) %declare variable ii to run from the row of length
if ~isempty(data{i})
cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
end
end
I need to read the EPS from EnduroSat, however i have difficulty understanding the line cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
Im required to utilised MatLab to code and this particular line have an error and i don't understand why.
Whenever you see a complicated line like this in MATLAB, try to break it up.
% find some indices. These values have been selected by the programmer/data, can't say why.
a=ALL_STRT(ii):(ALL_STRT(ii)+4)
% obtain that articular section of the data
b=data{i}(a)
% convert it to a char data type (characters)
c=char(b)
% scan text, and treat them as float
d=textscan(c,'%f')
% the output is a cell array, we want a matrix instead. Make the cell array into a matrix.
cmd_data(ii) = cell2mat(d)
You can read particularly what each of these do better in their documentation pages, and you can see it work if you put a break-point in the code, and see what each of this parts output when you call it. Learn how to debug, is a very very powerful tool

How to import a cell array data in MATLAB [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a set of data in a text file with the format like below. How shall I import the data into MATLAB? Thanks!
{(38.7948,1319,0.8571,119,0),(39.0693,138,0.0897,21,0),(40.7911,63,0.0409,7,0),(103.4512,19,0.0123,5,0),(-26.0424,223,1.0000,28,0)}
{(35.8689,110,0.5093,14,0),(47.7915,41,0.1898,7,0),(59.7489,53,0.2454,7,0),(71.7298,12,0.0556,3,0)}
It's always helpful if you in your question explain what you have tried so far, what the desired result would be or how you inteend to use it. That way the person answering the question don't have to assume a lot of things.
Here I assume that you would like to import each line as a cell with a set of number arrays within.
To get matlab to evalutate the expression correct the parentheses in the brackets need to be replaced
{(1,2),(3,4)}
Error: Expression or statement is incorrect
{[1,2],[3,4]}
ans =
[1x2 double] [1x2 double]
To read the file you could use fopen and then fgetl to get each line. When the result from fgetl isn't a char, the end of the file (EOF) is reached.
% Open file
f = fopen('...path\to\file.txt','r');
C = {};
while true
% Read each line
fStr = fgetl(f);
if ischar(fStr)
% Replace parentheses and evaluate expresission
C{end+1} = eval(regexprep(fStr,{'(',')'},{'[',']'}));
else
% End of file
break
end
end
fclose(f)
Perhaps you need to include some error checks if the data in your file should be formated incorrect. You could also check out other ways to read data, for exmple fread or fscanf

Matlab Classification load dataset [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to load and use a dataset in order to run some algorithms(Neural Networks) in Matlab. I've downloaded a dataset from the internet which has instances and attributes.
I've saved that dataset as a plain text file, and also with the extension .data or .mat. But I am not able to import and use it in Matlab.
How should I do? I also have to define a training and a test set after.
Thank you in advance.
I have to mention I am new to Matlab and trying to study it as a hobby.
You can just load the data by:
data = load('wine.data');
Then, you can split the data to training and testing very easily.
Here, I put 70% data for training and 30% for testing, but you could choose other fraction. 60-40 or 80-20
data = data(randperm(end), :);
traindata = data(1:floor(0.7*size(data, 1)), :);
testdata = data(floor(0.7*size(data, 1))+1:end, :);
In the end, when you want to run the classifier, remember that in this dataset, the first column is the label and the rest are features.

Searching through a large text file in Matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Assuming we use Matlab, what is the best way to search for a string in a large text file (can be bigger than 1GB)? Reading the entire file into memory would be costly.
You need to look into this:Importing Large text data.
The method would be to use blocks. Load Data in blocks.
It can be done by the range input argument in xlsread. In the range itself, you can specify the columns as well..
Syntax:
num = xlsread(filename,sheet,xlRange)
Example:
filename = 'myExample.xlsx';
sheet = 1;
xlRange = 'B2:C3';
subsetA = xlsread(filename, sheet, xlRange)

Import text file as a matrix in a matlab script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi everybody I need to automaticly import certain text files stored in my computer as matrices when I run my script in matlab. How do I do that? Thanks
Although the question shows little effort I am reminded how I started out with no knowledge about input or output whatsoever, it is a quite dense forest of information really.
Basically to read a file you need to:
Open the file
Read the file and assign it to a variable
Close the file
Some functions in MatLab take care of all three steps:
importdata
csvread
dlmread
The functions above are suitable if you have very neat and uniform data. Click the links to read if they are suitable for you. If your data is less uniform, e.g. it contains both numbers and letters, you might want to consider textscan.
Using textscan you must carry out all three steps yourself. First open your file and create a link to your file called a file ID (FID):
FID = fopen('mytextfile.txt')
Next you define a format specifier which describes a single line of data (a row).
formatSpec = '%f %f %f %f %s'
This format specifier represents 4 decimal numbers (floats) followed by a string all seperated by whitespace. For more information on the format specifier see:
http://www.mathworks.nl/help/matlab/ref/textscan.html#inputarg_formatSpec
Now you can read your text file by calling:
C = textscan(FID,formatSpec);
Which stores each column in a cell in C. So the first column is C{1}, the second C{2}, etc.
Finally make sure you close your file by using the file id:
fclose(FID);
Good luck!