Hi I am looking for a quick and dirty answer,
I try to make a surface or contourplot with gnuplot from a data file. The problem is the fileformat:
Its all in one row
line 1 till line 32 contain the values for the x-coordinates 1-32 and y=1,
line 33 is the value for x=1 and y=2 and so on...
I tried to do that with the "everyline" command but since they are not separated by a blank line this is not working.
Since this data file is an output file and from my program I get many of them, it is not practicable to modify each of them. It would be best if I find a way to do that directly with gnuplot.
I also tried it with "sed" but I am not yet further than extracing values from a specific line to a specific line.
If someone could help me with a quick applicable solution for this problem, it would be great.
You could use awk to add the needed columns, e.g.,
splot "<awk '{print int(NR/32),NR%32,$1}' datafile" u 1:2:3 with points
I may have switched the x and y columns around
Related
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)');
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.
I used to have a way to do this, but it's been lost to time... I have one program that outputs an undeliniated ascii data file, and another program that needs the data formatted it's own way. The output contains X,Y data points in the format:
X120207Y041009
X120107Y040071
etc. ...
where each of these ordinates represents a 2.4 data point. The input file needs it as such:
X 12.0207 Y 04.1009
X 12.0107 Y 04.0071
etc. ...
Not all of the lines in the file are data points, but the ones that are start with "X", have the exact same format, and contain nothing else on that line.
All my searching for a similar conversion points toward using sed as a quick, elegant way to do this, but I never learned sed. I think before I actually wrote a c program to do this conversion but that seems like the really hard way to recreate. If anyone could bail me out, I'll owe you a bagel!
You can make use of gawk's FIELDWIDTHS to handle fixed format text:
awk -v FIELDWIDTHS="1 6 1 6" '/^X/{for(x=2;x<=5;x+=2)sub(/../,"&.",$x)}7' file
Let's see an example:
kent$ cat f
X120207Y041009
X120107Y040071
we want to leave it, no matter what it is
kent$ awk -v FIELDWIDTHS="1 6 1 6" '/^X/{for(x=2;x<=5;x+=2)sub(/../,"&.",$x)}7' f
X 12.0207 Y 04.1009
X 12.0107 Y 04.0071
we want to leave it, no matter what it is
Edit 1 - This question has been solved and it was due to a typo thanks to Floris for spotting this.
I have a one line matrix in Matlab which it is truncating and causing me to loose data.
My code reads:
[status,Vf_rpm_string] = system (fragment_velocity_string);
Vf_rpm_shape=regexprep(Vf_rpm_string,'\n',' ');
Vf_rpm_vector=str2num(Vf_rpm_string);
Vf_rpm= reshape(Vf_rpm_vector,[],1);
The code conducts a system command and stores the result, the result is a Matrix of numbers and sometimes the last line in the matrix has less columns than the previous lines. Matlab doesn't like this, as it does not know what to do with the empty few columns in the last line. So I have to remove the new line character from the results (\n) and replace it with a space.
This was working fine until the results from the system command were too large and so when I remove the new line character (\n) and replace it with a space creating a one line matrix it is too long for Matlab and it truncates it and I loose a lot of my data. So when I convert the returned data (which is returned as a string) to a number it gives me an empty matrix, then the reshape command is pointless at this point.
This is how it reads in Matlab:
20.65866342... Output truncated. Text exceeds maximum line length of 25,000 characters for Command Window display.
So the 20.65866342 is the last value before I start to loose data. I know it says it is too large for the command window but still the variable does not store all the data and it is lost.
Does anyone have any solutions to avoid this truncation?
Or does anyone want to suggest an alternative method for me to convert my data?
I am using Matlab 2012b and Windows 7
Thanks for your time.
Could the problem be that you strip the newlines, but the stripped string isn't the one you are parsing?
[status,Vf_rpm_string] = system (fragment_velocity_string);
Vf_rpm_shape=regexprep(Vf_rpm_string,'\n',' ');
Vf_rpm_vector=str2num(Vf_rpm_string);
Vf_rpm= reshape(Vf_rpm_vector,[],1);
That third line of code should be
Vf_rpm_vector=str2num(Vf_rpm_shape);
if I understand the logic of your code.
I tried to plot graphs on MATLAB using GraphViz, using this GraphViz interface.
I keep getting this error:
>> [x,y]=draw_dot(G)
??? Attempted to access node_pos(2); index out of bounds because numel(node_pos)=1.
Error in ==> dot_to_graph at 94
y(lst_node) = node_pos(2);
Error in ==> draw_dot at 30
[trash, names, x, y] = dot_to_graph(tmpLAYOUT); % load NEATO layout
Whats really bugging me is that it worked great before (on my old computer).
Any idea how to solve this problem?
After debugging, I find the solution.
Just find the line 92 in dot_to_graph.m, as written:
[node_pos] = sscanf(line(pos_pos:length(line)), ' pos = "%d,%d"')';
Change the %d,%d to %f,%f. Because there are float numbers in the dot file.
This is difficult to answer completely since you are not giving us the G you are using, so we can't reproduce your problem directly; I'll attempt to answer anyhow "on the dry":
The error messages you get mean that the temporary DOT files created by neato in draw_dot can not be read properly; a line in the dot file which is parsed by dot_to_graph using the format string pos = "%d,%d" is expected to contain two numbers, e.g pos ="42,3", but MATLAB's sscanf only reads one number from that line.
Is it possible that your new computer uses a different language setting, i.e. using a decimal comma instead of a decimal point? This might cause Matlab to read the two numbers as one, not sure how sscanf adapts to local decimal point settings.
Otherwise, are you still using the same version of neato as before? Could it be that its output format has changed in some way?
The best way to find out might be to set a debugging break point in the offending line 94 ([node_pos] = sscanf(line(pos_pos:length(line)), ' pos = "%d,%d"')';) and check what line(pos_pos:length(line)) evaluates to.