MATLAB workspace not showing variables - matlab

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 ?

Related

Read a big data file with headlines into a matrix

I have a file that looks like this (with real data and much bigger):
A B C D E F G H I
1 105.28 1 22 84 2 10.55 21 2
2 357.01 0 32 34 1 11.43 28 1
3 150.23 3 78 22 0 12.02 11 0
4 357.01 0 32 34 1 11.43 28 1
5 357.01 0 32 34 1 11.43 28 1
6 357.01 0 32 34 1 11.43 28 1
...
17000 357.01 0 32 34 1 11.43 28 1
I want to import all the numerical value into a matrix, skipping the headlines. For that purpose I use this code:
Filename = 'test.txt';
A = dlmread(Filename,' ',1,0); %Imports the whole data into a matrix
The problem with this is just that A is a 17 000 * 1 vector instead of a matrix with several columns. If I manual edit the data file, remove the headlines and just run this it works:
A = dlmread(Filename); %Imports the whole data into a matrix
But I would prefer not to do this since the headlines are used later on in the code. Any advice how to get this work?
edit: solved by using
' '
instead of just
' '
Use the import tool.
Make sure you choose the data.
Generate script.

Find the difference between positive and negative peaks MATLAB

I need to find the difference between positive and negative peaks where the difference is greater than +-3.
I am using findpeaks function in MATLAB to find the positive and negative peaks of the data.
In an example of my code:
[Ypos, Yposloc] = findpeaks(YT0);
[Yneg, Ynegloc] = findpeaks(YT0*-1);
Yneg = Yneg*-1;
Yposloc and Ynegloc return the locations of the positive and negative peaks in the data.
I want to concatenate Ypos and Yneg based on the order of the peaks.
For example, my peaks are
Ypos = [11 6 -10 -10 6 6 6 6 6 -5]
Yneg = [-12 -14 -11 -11 -11 5 5 5 -6]
Locations in YT0
Yposloc = [24 63 79 84 93 95 97 100 156]
Ynegloc = [11 51 78 81 85 94 96 99 154]
In this case, where both Yposloc and Ynegloc are 9x1, I can do the following;
nColumns = size(Yposs,2);
YTT0 = [Yneg, Ypos]';
YTT0 = reshape(YTT0(:),nColumns,[])';
YTT0 = diff(YTT0)
YT0Change = numel(YTT0(YTT0(:)>=3 | YTT0(:)<=-3));
Total changes that I am interested is 6
However, I need to concatenate Yneg and Ypos automatically, based on their locations. So I think I need to to do an if statement to figure out if my positive or negative peaks come first? Then, I am not sure how to tackle the problem of when Ypos and Yneg are different sizes.
I am running this script multiple times where data changes and the negative/positive peak order are constantly changing. Is there a simple way I can compare the peak locations or am I on the right track here?
I would check each minimum with both the previous and the next maxima. In order to do that you can first combine positive and negative peaks according to their order:
Y = zeros(1, max([Yposloc, Ynegloc]));
Yloc = zeros(size(Y));
Yloc(Yposloc) = Yposloc;
Yloc(Ynegloc) = Ynegloc;
Y(Yposloc) = Ypos; % I think you inserted one extra '6' in your code!
Y(Ynegloc) = Yneg;
Y = Y(Yloc ~= 0) % this is the combined signal
Yloc = Yloc(Yloc ~= 0) % this is the combined locations
% Y =
%
% -12 11 -14 6 -11 -10 -11 -10 -11 6 5 6 5 6 5 6 -6 -5
%
% Yloc =
%
% 11 24 51 63 78 79 81 84 85 93 94 95 96 97 99 100 154 156
And then calculate the differences:
diff(Y)
% ans =
%
% 23 -25 20 -17 1 -1 1 -1 17 -1 1 -1 1 -1 1 -12 1
If you want changes of at least 6 units:
num = sum(abs(diff(Y)) > 6)
% num =
%
% 6
Ypos = [11 6 -10 -10 6 6 6 6 -5];
Yneg = [-12 -14 -11 -11 -11 5 5 5 -6];
Yposloc = [24 63 79 84 93 95 97 100 156];
Ynegloc = [11 51 78 81 85 94 96 99 154];
TOTAL=[Yposloc Ynegloc;Ypos Yneg];
%creatin a vector with positions in row 1 and values in row 2
[~,position]=sort(TOTAL(1,:));
%resort this matrix so the values are in the orginial order
TOTAL_sorted=TOTAL(:,position);
%look at the changes of the values
changes=diff(TOTAL_sorted(2,:));
if changes(1)>0
disp('First value was a Minimum')
else
disp('First value was a MAximum')
end
%same structure at the TOTAL matrix
%abs(changes)>3 produces a logical vector that shows where the absolute values was bigger
%than 3, in my opinon its rather intresting where the end is then were the start is
% thats why i add +1
Important_changes=TOTAL_sorted(:,find(abs(changes)>3)+1);
plot(TOTAL_sorted(1,:),TOTAL_sorted(2,:))
hold on
plot(Important_changes(1,:),Important_changes(2,:),...
'Marker','o','MarkerSize',10, 'LineStyle','none');
hold off

Simulink: Creating a repeating irregular square wave

I want to genereate a square wave to represent different uptimes for a lightning installation over a year.
The schedule over a week is the following:
Mon-Thu: 06.00-20.00
Fri: 06.00-18.00
Sat: no uptime
Sun: no uptime
So my wave should repeat every 168 hours (one week) and look like this:
Time Signal
0-6 0
6-20 1
20-30 0
30-44 1
44-54 0
54-68 1
68-78 0
78-94 1
94-104 0
104-116 1
116-168 0
I've tried some with the repeating sequence block using the following:
Time values:[0 6 6.001 20 20.001 30 30.001 44 44.001 54 54.001 68 68.001 78 78.001 94 94.001 104 104.001 116 116.001 168]
Output values: [0 repmat([0 1 1 0],1,5) 0]
But since I'm simulating over 8760 (a year in hours) it seems that the step is f*cking things up.
Is there any better way or good way to make this work?
Thanks a bunch.

Matlab figure and truncating data

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))

matlab dbstop if naninf -> recursion limit and crash

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>>