Extract Data points of a contourplot in Maple17 - maple

I am working with Maple (17). I am quite new at it and I need to learn or get to know how extract the raw data or data points from a contourplot (2d plot) to a text file or whatever, but just get the data and later plot in other software.

Maple's 2D plotting commands construct function calls in Maple's own language, which can be examined using stock commands (eg. nops, op, lprint, select, etc).
Of course the layout and nature of the contents of these PLOT function calls (which server as data structure) vary according to which particular plotting command produces them.
Here is some basic poking at the contents of the PLOT function call returned by the plots:-contourplot command.
restart;
P := plots:-contourplot( sin(x)*y^2,
x=-Pi..Pi, y=-1..1,
contours=[-1/2,-1/7,1/7,1/2],
grid=[39,39] ):
# The GUI prints the PLOT structure by rendering it graphically.
P;
# Data for 2D curves are stored within substructures that are
# function calls to the name `CURVES`.
Pdata := select( type, [op(P)], specfunc(anything,CURVES) ):
# There are four CURVES within this particular structure
# assigned to P, corresponding to the 4 named contour values.
nops(Pdata);
4
# Select only the numeric data, in a list.
C1 := select( type, [op(Pdata[1])], list(list(numeric)) ):
# There happen to be 78 entries of data.
nops( C1 );
78
# Print the first five entries in list C1.
# Each one is a list of two lists. In each such pair
# the two lists each contain two floats, representing
# x and y coordinates of a point.
# Each list of two points represents a line segment.
# This first CURVE is stored as 78 individual line segments
# (but here is the end-point data for 5 of those).
map(print, C1[1..5]):
[[0.524804672715281, -1.], [0.560425257062589, -0.967862584000641]]
[[0.593087222168692, -0.947368421052632], [0.560425257062589, -0.967862584000641]]
[[0.593087222168692, -0.947368421052632], [0.611600658135984, -0.931520641144355]]
[[0.661387927071535, -0.902063357909845], [0.611600658135984, -0.931520641144355]]
[[0.661387927071535, 0.902063357909845], [0.593087222168693, 0.947368421052631]]
You could look at the commands Export, writedata, fprintf, or ExportMatrix for choices about exporting such data to files. But of course first you'd need to decide on your target format.

Related

Retrieve Gradient of Reference Line Generated by probplot

I am generating probability plots for a number of data sets in matlab.
I am plotting them using probplot with a weibull distribution reference line
data = [1,1,1,1,2,2,2,3,4,5,3,3,2,2,1,3,5,7,2,4,2] ;
h = probplot('weibull',data) ;
This function as per the matlab documentation returns a graphic array object. This appears to only contain the original data and not the reference line.
Is there any way of retreiving information about about this reference line without plotting it and indiviually extracting it using the figure tools (very much not an option I'd like to go down as there are potentionally hundreds of plots to go through).
I can see there is wblplot that returns a line array of 3 lines, one of which is the original data and one of the others is likely the reference the line however I will have to try different distributions to fit further down the road and would prefer to keep a generic approach.
You are wrong!
data = [1,1,1,1,2,2,2,3,4,5,3,3,2,2,1,3,5,7,2,4,2] ;
h = probplot('weibull',data) ;
b=h(2);
figure
plot(b.XData,b.YData)
h is a graphic array object, so its an array. The first element contains the original data, but the second h(2) contains the reference line.

How to use binning method for identifying the incoming point belongs to which bin?

I have small query. I have two data sets. In one data sets for example I did binning and calculated the mean value and std value along with group binning. Now in I have second data sets of same parameters say X. I would like identify this X data sets belong to which bin groups of my previous data sets using matlab.
Could you give some example how to identify the incoming data points belongs to which bin group...??
I used following binning which is available in matlab :
binEdges = linspace(botEdge, topEdge, numBins+1);
[h,whichBin] = histc(x, binEdges);
Well... you already have your bin edges. Anything inside specific edges is in that bin.
If you know that the data is inside the ranges you defined then, for each new data
newdatabin=find(newdata>binedges,1,'last'); %this is the bin number where the new data goes in
h(newdatabin)=h(newdatabin)+1; %add one!
Also consider using histcounts if your MATLAB version is new enough.

Splitting non-continuous sized matrix in vectors

I'm writing an piece of software within Matlab. Here, the user can define a dimension say 3.
This dimension is subsequently the number of iterations of a for loop. Within this loop, I construct a matrix to store the results which are generated during every iteration. So, the data of every iteration is stored in a row of a matrix.
Therefore, the size of the matrix depends on the size of the loop and thus the user input.
Now, I want to separate each row of this matrix (cl_matrix) and create separate vectors for every row automatically. How would one go on about? I am stuck here...
So far I have:
Angle = [1 7 15];
for i = 1:length(Angle)
%% do some calculations here %%
cl_matrix(i,:) = A.data(:,7);
end
I want to automate this based on the length of Angle:
length(Angle)
cl_1 = cl_matrix(1,:);
cl_7 = cl_matrix(2,:);
cl_15= cl_matrix(3,:);
Thanks!
The only way to dynamically generate in the workspace variables variables whos name is built by aggregating string and numeric values (as in your question) is to use the eval function.
Nevertheless, eval is only one character far from "evil", seductive as it is and dangerous as it is as well.
A possible compromise between directly working with the cl_matrix and generating the set of array cl_1, cl_7 and cl_15 could be creating a structure whos fields are dynamically generated.
You can actually generate a struct whos field are cl_1, cl_7 and cl_15 this way:
cl_struct.(['cl_' num2str(Angle(i))])=cl_matrix(i,:)
(you might notice the field name, e. g. cl_1, is generated in the same way you could generate it by using eval).
Using this approach offers a remarkable advantage with respect to the generation of the arrays by using eval: you can access to the field od the struct (that is to their content) even not knowing their names.
In the following you can find a modified version of your script in which this approach has been implemented.
The script generate two structs:
the first one, cl_struct_same_length is used to store the rows of the cl_matrix
thesecond one, cl_struct_different_length is used to store arrays of different length
In the script there are examples on how to access to the fileds (that is the arrays) to perform some calculations (in the example, to evaluate the mean of each of then).
You can access to the struct fields by using the functions:
getfield to get the values stored in it
fieldnames to get the names (dynamically generated) of the field
Updated script
Angle = [1 7 15];
for i = 1:length(Angle)
% do some calculations here %%
% % % cl_matrix(i,:) = A.data(:,7);
% Populate cl_matrix
cl_matrix(i,:) = randi(10,1,10)*Angle(i);
% Create a struct with dinamic filed names
cl_struct_same_length.(['cl_' num2str(Angle(i))])=cl_matrix(i,:)
cl_struct_different_length.(['cl_' num2str(Angle(i))])=randi(10,1,Angle(i))
end
% Use "fieldnames" to get the names of the dinamically generated struct's field
cl_fields=fieldnames(cl_struct_same_length)
% Loop through the struct's fileds to perform some calculation on the
% stored values
for i=1:length(cl_fields)
cl_means(i)=mean(cl_struct_same_length.(cl_fields{i}))
end
% Assign the value stored in a struct's field to a variable
row_2_of_cl_matrix=getfield(cl_struct_different_length,(['cl_' num2str(Angle(2))]))
Hope this helps.

How can I apply Huffman coding correctly?

I applied the zigzag function after quantization to an image block, and I want to compute the Huffman coding of this block. I understand that the input argument must be a vector, and that the histogram should be calculated.
I wrote the following code, but it doesn't seem to work:
[M N]=size(yce);
fun1=zigzag(yce);
count1 = imhist(fun1);
p1 = count1/ numel(fun1);
[dict1,avglen1]=huffmandict(count1,p1);
comp1= huffmanenco(fun1,dict1);
Im1 = huffmandeco(comp1,dict1);
I get the following error with the huffmandict function:
Error in project at 65
[dict1,avglen1]=huffmandict(count1,p1);
Source symbols repeat.
zigzag.m is a written function in a matlab file.it converts a matrix into a vector,thus eliminating long sequences of zeros.
The Huffman encoding function (huffmandict) in MATLAB requires that the symbols vector (first argument of the function) must all be unique values. This symbols vector is a list of all possible symbols that are seen in your data that you want to encode / compress. As such, it wouldn't make sense to have a list of all symbols to be encountered if there are duplicates. This is much like a dictionary of words, where it wouldn't make sense to see the same word twice in this dictionary. The second parameter of the function is the associated probabilities of occurrence for each symbol in your sequence.
With huffmandict, what you are doing is you are creating a dictionary for Huffman encoding that consists of all possible unique symbols to be encountered when encoding/decoding as well as their associated probabilities. Therefore, by examining your code, you need to extract both the bin locations as well as the probabilities of occurrence when using imhist. Essentially, you need to call the two element output version of imhist. The second output of imhist gives you a list of all possible intensities / symbols that were encountered in the data, while the first element gives you the frequency of each these intensities / symbols in your data. You then normalize the first output element by the total number of symbols / intensities in your data to get the probabilities (assuming equiprobable encounters of course). Once this is complete, you use both of these as input into huffmandict.
In other words, you need to change only two lines of code, thus:
[M N]=size(yce);
fun1=zigzag(yce);
[count1,x] = imhist(fun1); %// Change
p1 = count1/ numel(fun1);
[dict1,avglen1]=huffmandict(x,p1); %// Change
comp1= huffmanenco(fun1,dict1);
Im1 = huffmandeco(comp1,dict1);
Edit
Knowing how fun1 is structured now, do not use imhist. imhist assumes that you are putting in image data, but it doesn't look like that's the case. Instead, try using histc instead to compute the frequency of occurrence. As such, simply modify your code to this:
[M N]=size(yce);
fun1=zigzag(yce);
bins = unique(fun1); %// Change
count1 = histc(fun1, bins); %// Change
p1 = count1/ numel(fun1);
[dict1,avglen1]=huffmandict(bins,p1); %// Change
comp1= huffmanenco(fun1,dict1);
Im1 = huffmandeco(comp1,dict1);
unique finds those unique values that are in your vector so that we can use these as bins to calculate our frequencies. This also figures out the all possible symbols seen in the data.

For loop to extract object from structures of different lengths

I have a .mat file with a 1x200 structure (corresponding to 200 experimental sessions). In each of these cells there is a matrix of different lengths (made up of 600-800 1x1 structures, corresponding to the number of trials in each session). Within each of these structures (trials) there are single values which I wish to store separately. How should I go about this?
allData <1x200 struct> (All sessions)
allData(1,1) <1x1 struct> (1 session)
allData(1,1).trial <600-800x1 struct> (All trials in 1 session, # of trials is variable)
allData(1,1).trial(1,1).value (What I want to store)
Thanks a lot!
I think you are looking for a nested loop:
OUTPUT = [];
for I=1:size(allData,2)
for J=1:size(allData(1,I).trial,1)
OUTPUT(end+1) = allData(1,I).trial(J,1).value;
end
end
Additional explanations:
When using the size function in Matlab for-loop statements, it is important to check (if necessary in the debug mode) that the correct dimension is selected. That is why, based on the data format in question here, it says size(X,2) first, because it is a 1x200 structure, so we are interested in the 2nd dimension; and size(Y,1) second, because it is a 600x1 structure.
As the overall dimension of OUTPUT may be difficult to determine a priori, it is initialized as empty vector. This is important for the (end+1) 'counter' to work, which will fail, if the variable isn't know when the command is called first. Thereafter, it will simply append the value to OUTPUT.