How can I read a text file which has a strings n the first row and numbers and symbols in the next rows on octave? - matlab

I have a text file which contains titles for each column on the first row, and then numbers and symbols (such as "pi" for pi=3.14 or a distance represented with "d" or angle represented with "beta") And I
The text file is as follows:
Example of text file to be read
It works perfectly in MATLAB using readtable('text.txt') and I obtain the following:
Result in MATLAB
But, I am not able to get the same result in Octave. Is there a function in Octave that allows me to extract all the information from the text file similarly to MATLAB?
Thank you

you can read such file using textread in octave, see
https://octave.sourceforge.io/octave/function/textread.html

Related

Can i write out a txt or csv doc with data of varying dimensions in Matlab?

I am using Matlab R2013b.
I have a 100x100 matrix which contains both numbers and strings. I converted it to a cell array (alldat) and wrote it to a csv file (blah.csv).
I then tried to append a single number to the top line of this csv file...which Matlab won't let me do.
cell2csv('blah.csv',alldat)
I can append the single number 'n' at the bottom of the matrix:
dlmwrite('blah.csv',n,'-append','delimiter',' ','roffset',1)
But it won't let me do it the other way around (so I can put the number in the first cell of the csv file, then have the matrix below it.
Can anyone advise?
I also tried outputting the cell array to a txt document using dlmwrite:
dlmwrite('blah.txt',alldat,'delimiter',' ');
And I kept getting this error:
Error using dlmwrite (line 113) The input cell array cannot be
converted to a matrix.
I often use tables for such tasks. Since you have a 100 x 100 array and not variables with different dimensions, it should be possible to adapt.
VarA={'12A3';123;'12B3'};
VarB={'45A6';456;'45B6'};
T=table(VarA,VarB);
writetable(T,'test.csv','WriteVariableNames',false)
T1=readtable('test.csv','ReadVariableNames',false)
You may want to use cell2table to create a table directly from your cell array, although it didn't work for me because it made some strange conversions from number to character.

How to plot .txt file

I have a text file with two columns, called Fx1. In column 1 it shows deflection calculations and in column 2 it shows force calculation (about 100).
I loaded the text file into matlab as a variable, called Fx1.
How do I plot this text file as a graph, with deflection as my x value, and force as my y value? Apparently, I am supposed to define my variables, but I do not know how to do so when I'm getting the data from a .txt file.
This is what I did and I did not get the correct graph:
plot(fx1)
Any ideas?
Here is a screenshot of my text file:
Here is my text file. It continues for many values, I just copy and pasted the beginning
Here is a screenshot of my whole workspace, I am trying to make a plot for all txt files between Fx1-Fx9.
Matlab work space
You should separate fx1 into two vector. Use these commands:
x=fx1(:,2)';
y=fx1(:,1)';
plot(x,y);
In additional you can combine these three command to one command:
plot(fx1(:,2)',fx1(:,1)');

extra lines in command window output

I am very new to MATLAB and i am currently trying to learn how to import files in matlab and work on it. I am importing a "*.dat" file which contains a single column of floating point numbers[they are just filter coefficients I got from a c++ code] into an array in MATLAB. When I am displaying the output in command window the first line is always " 1.0e-03 * " followed by the contents of my file. I want to know what it means? When I check my workspace the array connects the correct number of inputs. My sample code and first few lines of output are below:
Code:-
clear; clc;
coeff = fopen('filterCoeff.dat');
A = fscanf(coeff, '%f');
A
fclose(coeff);
Output:-
A =
**1.0e-03 *** <===== What does this mean?
-0.170194000000000
0
0.404879000000000
0
-0.410347000000000
P.S: I found many options to read file eg. textscan, fscanf etc. Which one is the best to use?
It is a multiplier that applies to all the numbers displayed after that. It means that, for example, the last entry of A is not -0.410347 but -0.410347e-3, that is, -0.000410347.
I think it is is just Matlab's display number type. It means each of your results are scaled by that amount.
format longg
A
And see what it displays. Look at the docs for format for other options.

Is there a compact view for matrices in matlab?

I want to have a look at a large matrix in MATLAB such that all columns are printed in one single line rather than spread out over several lines.
Is such thing possible? That would be great to know.
Try disp(matrixName(:)). The matrixName(:) command turns your matrix into a long vector in column-major order, so it basically just shows you the first column, followed by the second, the third, etc.
If that does not do the trick, you could look into the doprint command.
EDIT: You could also save the matrix to a text file and view the file. You do this like so:
fileID = fopen('C:/path/to/file/myMatrix.txt');
fprintf(fileID, formatString, myMat);
fclose(fileID);
fopen documentation
fprintf documentation
Additional information can be found here
The formatString variable in the above tells fprintf how the data should be displayed. If you have a really big matrix with tons of columns, where all of the values are floats, the easiest way to create this string is to use something like:
formatString = strcat(repmat('%f ', 1, size(myMat, 2)), '\n');
This will create a long string specifying that each element in your matrix is a float, and where it goes, and then cap it off with a line feed so that the next row of your matrix starts on the next line.
Suppress your original matrix with a semicolon and then use the "disp" command to show your matrix however you want.
for i = 1 : length(matrix(1,:))
disp(matrix(:,i))
end
Some "obvious" answers:
You can choose a smaller font - then more values will fit in a line
You can play with the format command to have less digits displayed
(my favourite) Use the variable viewer - via "open selection" or Ctrl-D when the name of a variable is highlighted. This will show your matrix in an excel-like table.

Matlab: reading from a .csv file

I am trying to import some data from a .csv file, I have search for solutions but no one seems to solve my problem. My .csv is just one column of numbers, but when I try to read it with csvread('myfile.csv') it says that it cannot convert from string. When I double click on the .csv file in matlab I can see that every number from the .csv has this aspect:
"996.47"
So every number is between double commas, and whatever I do I can not get just the number between them. I am trying also opening the file and with textscan but I find no way. Thank you very much in advance.
You can try this workaround:
V = dlmread('myfile.csv','"');
v = V(:,2)
According to your description you have one column of values formatted like "996.47". The first line creates a matrix where columns are delimited by '"' - you get three columns where the middle one is filled with your values. The second line extracts the middle column.
what about using
importdata('yourfile.csv')
It should work if you are only interested in data.
If you want a more generic solution that doesn't need to deal with indexing, you can use MATLAB's built-in function importdata.
x = importdata('yourfile.csv'); % reads in the file as text surrounded by double quotes
x = cellfun(#str2num,strrep(v,'"','')); % removes the double quotes and changes text to numbers