I issue following command in the interactive MATLAB console:
>> foo = [1 inf];
>> dbstop if naninf
>> foo
I now get weird behaviour: MATLAB seems to to break into two different files, but doesn't actually stop execution. This is pretty slow because the editor switches between those two files repeatedly, Ctrl+C doesn't do anything. Output is:
481 end
20 if ~isfloat(value)
20 if ~isfloat(value)
399 if numel(var) > numelLimit
20 if ~isfloat(value)
20 if ~isfloat(value)
399 if numel(var) > numelLimit
20 if ~isfloat(value)
20 if ~isfloat(value)
399 if numel(var) > numelLimit
...
...
it then finally stops with a debug prompt, with a really long (recursive) stack like:
dbstack
In codetools/private/dataviewerhelper>upconvertIntegralType at 20
In codetools/private/dataviewerhelper at 9
In workspacefunc>createComplexScalar at 271
> In workspacefunc>num2complex at 241
In workspacefunc>getShortValueObjectJ at 230
In workspacefunc>getShortValueObjectsJ at 349
In workspacefunc at 21
In codetools/private/dataviewerhelper>upconvertIntegralType at 20
In codetools/private/dataviewerhelper at 9
In workspacefunc>createComplexScalar at 271
In workspacefunc>num2complex at 241
In workspacefunc>getShortValueObjectJ at 230
In workspacefunc>getShortValueObjectsJ at 349
In workspacefunc at 21
In workspacefunc>getStatObjectsJ at 399
In workspacefunc at 27
In codetools/private/dataviewerhelper>upconvertIntegralType at 20
In codetools/private/dataviewerhelper at 9
In workspacefunc>createComplexScalar at 271
In workspacefunc>num2complex at 241
In workspacefunc>getShortValueObjectJ at 230
In workspacefunc>getShortValueObjectsJ at 349
In workspacefunc at 21
In codetools/private/dataviewerhelper>upconvertIntegralType at 20
In codetools/private/dataviewerhelper at 9
In workspacefunc>createComplexScalar at 271
In workspacefunc>num2complex at 241
In workspacefunc>getShortValueObjectJ at 230
In workspacefunc>getShortValueObjectsJ at 349
In workspacefunc at 21
In workspacefunc>getStatObjectsJ at 399
In workspacefunc at 27
...
...
In my real program I'm trying to debug I get the same but even worse so that sometimes I hit the recursion limit error and abort, sometimes MATLAB simply completely crashes. I would really like to be able to use dbstop if naninf, but this makes it pretty much impossible and this makes me sad. Any advice?
Using MATLAB 2009b 64 bit on Linux.
Thanks!
Edit:
I just tried it on MATLAB 2007b 32 bit Linux:
>> foo = [1 inf]
foo =
1 Inf
>> dbstop if naninf
>> foo
foo =
1 Inf
>> foo = [1 inf]
foo =
1 Inf
>>
>> t = foo(2)
t =
Inf
So here dbstop if naninf doesn't seem to do anything when deliberately assigning inf to a variable. The docs say:
dbstop if naninf or dbstop if infnan stops execution when any MATLAB program file you subsequently run produces an infinite value (Inf) or a value that is not a number (NaN) as a result of an operator, function call, or scalar assignment, putting MATLAB in debug mode, paused immediately after the line where Inf or NaN was encountered.
Shouldn't this hit even when I deliberately assign a inf to a variable (as in above t = foo(2) or s = inf) or what is meant by "scalar assignment"?
That weird deeply recursive breakpoint you're seeing looks like you're hitting breakpoints in the part of the Matlab GUI that is itself implemented in M-code, when it's trying to display NaN or Inf values in your workspace. (This is one of the downsides of the Matlab IDE running in the Matlab VM along with user code.) I can reproduce. Try turning off the Workspace view in the Desktop menu, or switching to a minimal layout with Desktop > Desktop Layout > Command Window Only.
For the second part: the breakpoint won't be hit for expressions entered directly at the command line. If you throw it in a script or function you'll hit the breakpoint. For example:
function repro_dbstop_naninf
foo = Inf;
foo = [1 Inf];
bar = foo(2);
disp('last line');
When you invoke this function, it'll break on (after, actually) lines 2 and 4.
>> dbstop if naninf
>> repro_dbstop_naninf
NaN/Inf breakpoint hit for repro_dbstop_naninf on line 2.
Stopping at next line.
2 foo = Inf;
3 foo = [1 Inf];
K>>
Related
I tried to import an excel file into my matlab program and tried to extract data from it:
clc;
clear all;
load lab3q2.mat
t = lab3q2(:,1);
v = lab3q2(:,2);
i = lab3q2(:,3);
plot(t,v)
hold on;
plot(t,i)
Everytime I run this script, I get command window output:
>> lab3q2
lab3q2 =
0 0 -12
10 325 0
20 325 12
30 325 12
40 0 12
50 -325 0
60 -325 -12
70 -325 -12
80 0 -12
However, I do not get any plot. Surprisingly, the workspace doesn't register the variables t,v,i. This seemed to suggest that the script isnt running...but then if that were the case, I wouldn't have got command window output..
What exactly is going on? Why am I not getting any plot? Why isn't the workspace registering the variables t,v,i ?
xlim command changes the axis limit of the figure.
How can i also limit data that is contained by the figure?
Apparently, even though xlim is applied data is still there.
Example: let's say i have a data set of 5000 elements. but only 1500 elements are shown in a figure. when i save this figure, it will still contain data that is not shown in the figure.
The answer may be particularly useful for people working with matlab2tikz.
Yes, the data are still there. To remove data, use something like this:
>> plot(1:10,(1:10).^2); % just an example
>> h = get(gca,'Children');
>> x = get(h,'XData')
x =
1 2 3 4 5 6 7 8 9 10
>> y = get(h,'YData')
y =
1 4 9 16 25 36 49 64 81 100
>> set(h,'XData',x(2:5), 'YData',y(2:5))
>> set(h,'XData',x(2:5), 'YData',y(2:5))
I have two arrays threshold and values.
threshold=[10 22 97]
values=[99 23 77 11 8 10]
I want to output idx such that threshold(idx-1)<values(i)<=threshold(idx). That is for the above example output will be
output=[4 3 3 2 1 1]
The naive code that can produce above output will be
output=ones(1,length(values))*(length(values)+1);
for i=1:length(values)
for j=1:length(threshold)
if(values(i)>threshold(j))
output(i)=j;
end
end
end
Is there a simple way of doing it. I want to avoid loops.
You can use histc command, with a slight adjustment of threshold array
>> threshold=[-inf 10 22 97 inf];
>> values=[99 23 77 11 8 10];
>> [~, output] = histc( values, threshold+.1 )
output =
4 3 3 2 1 1
The modification of threshold is due to "less-than"/"less-than-equal" type of comparison for bin boundary decisions.
No loops often means you'll gain speed by increasing peak memory. Try this:
threshold = [10 22 97];
values = [99 23 77 11 8 10];
%// Do ALL comparisons
A = sum(bsxfun(#gt, values.', threshold));
%// Find the indices and the ones before
R = max(1, [A; A-1]);
%// The array you want
R(:).'
If you run out of memory, just use the loop, but then with a find replacing the inner loop.
Loops aren't all that bad, you know (if you have MATLAB > R2008). In theory, the solution above shouldn't even be faster than a loop with find, but oh well...profiling is key :)
I wanted to plot the following
y=linspace(0,D,100)
temp=y^2;
plot(y,temp);
i am getting an error with y^2, it says matrix should be square.
is there another way to plot.
You are not getting that error because of plot. You are getting it because of
temp=y^2
Instead, you should be using
temp=y.^2
^ means matrix power. .^ is elementwise power. You can find more about MATLAB operators here.
Let's say you have a 3x3 matrix, magic(3).
A=magic(3)
A =
8 1 6
3 5 7
4 9 2
Here is square of matrix A (which is A*A, as Dan suggested):
A^2
ans =
91 67 67
67 91 67
67 67 91
Here is the matrix which contains squares of A's elements:
A.^2
ans =
64 1 36
9 25 49
16 81 4
Just as an alternative to the above answer, you can consider the following case:
A = magic(3);
temp = bsxfun(#times,A,A);
which retrieves the same results as
temp = A.^2;
the . operator will apply your square element-wise. bsxfun makes exactly the same.
I hope this helps.
I'm trying to monitor the average temperature in a fabrication every hour to ensure quality control. How can I write a script that looks at the temperature inside the plant as a function of time, and outputs the times when the temperature drops below 10 degrees Celsius and when the temperature is above 80 degrees Celsius. My script should say when the temperature is out of the boundary and what the temperature is. I wanna Use the following data:
Temperature = [-15 -5 5 15 24 33 42 51 59 66 73 79 85 90 78]
The first measurement is made at 5am, the last measurement is made at 7pm. I wanna display the time in a 24 hour system instead of a 12 hour system.
It looks like you'll need to loop through the elements of the Temperature vector and find which ones are either below 10 degrees OR above 80 degrees. In a traditional programming language you would use a FOR loop to go through the elements of an array or vector, but generally in MATLAB you want to avoid FOR loops if you can and instead take advantage of MATLAB's vectorization, because it's much faster.
You'll want to look into the FIND function (type 'help find' into the console for more information). But, for example if I had a vector:
A = [0 1 2 1 2 1 1 0];
And used
find(A==0)
The output would be a vector of the indices of A where the element is equal to 0:
[1 8]
I could similarly do:
find(A==1 & A==0)
And I would get
[1 2 4 6 7 8]
This is useful because while traditionally you access the elements of a vector with an index, you can access the elements of a vector in MATLAB with another vector. For example:
>> A = [-10 4 -2 3];
>> ind = [2 3];
>> A(ind)
ans =
4 -2
MATLAB also makes the syntax a bit easier, because you can use the following shortcut instead of explicitly using the FIND function:
>> A = [-10 4 -2 3];
>> A( A<-5 | A>3)
ans =
-10 4
Which would be the same as using the FIND function:
>> A(find(A<-5 | A>3))
ans =
-10 4
I hope this helps. Sorry for the long post. It takes some time to get used to MATLAB's vectorized way of writing code, but once you do get used to it, you'll find it's very useful for computation.
You could crate a 'time' vector like this:
time = 5:1:19;