My last elseif statement does not execute even if the conditions are met:
Currency_Exchanage != 'Select...' and all other variables (ETF_Exchanage, Index_Exchanage and Stock_Exchanage) = 'Select...'
Here is the section of code that I am concerned about:
if (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1)
if db == 1 && uni == 1
tickers = gnr_bloomberg; % Analsise Bloomberg natural resources
nrm=1;
elseif db == 1 && uni == 2
tickers = all_bloomberg; % Analsise Bloomberg all
nrm=1;
elseif db == 2 && uni == 1
tickers = gnr_yahoo; % Analsise Yahoo natural resources
nrm=1;
elseif db == 2 && uni == 2
tickers = all_yahoo; % Analsise Yahoo all
nrm=1;
end
else
%Yahoo inputs
if (strcmp(ETF_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from ETF
tickers = ETF_Yahoo(:,1);
Exchanges = ETF_Yahoo(:,2);
Exchange = ETF_Exchanage;
db=2; %Yahoo Selection
elseif (strcmp(Index_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(ETF_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Index
tickers = Index_Yahoo(:,1);
Exchanges = Index_Yahoo(:,2);
Exchange = Index_Exchanage;
db=2;
elseif (strcmp(Stock_Exchanage,'Select...') == 0) && (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Stock
tickers = Stock_Yahoo(:,1);
Exchanges = Stock_Yahoo(:,2);
Exchange = Stock_Exchanage;
db=2;
elseif (strcmp(Currency_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(ETF_Exchanage,'Select...') == 1); %Choose exhanges from Currency
tickers = Currency_Yahoo(:,1);
Exchanges = Currency_Yahoo(:,2);
Exchange = Currency_Exchanage;
db=2;
else
msg = 'Error occurred.\Only one Yahoo input menue must be used!';
error(msg)
end
end
Any Help would be much appropriated, I can't see where I'm going wrong here. I am using Matlab 2013a.
Put a breakpoint at the elseif statement in question and then check in the command window what your condition evaluates to.
If it does not evaluate like expected, check what the individual terms evaluate to.
It is important to actually test what the conditions evaluate to in matlab, rather than only visually comparing the string values.
Usually by that point you should get a rough idea what is wrong.
However in your case we can't do these steps for you because something is off. Your code condensed to the more reasonable minimal example
if 1 && 1 && 1...
1;
disp('I was here')
end
does not even execute in R2014a since the interpreter complains about '...' being an unexpected matlab expression.
Related
Despite using isscalar, isinteger... the result after running is still true, I still had this test which is incorrect and in relation with Scalar values.
My program:
function valid = valid_date(year, month, day)
[y1 y2] = size(year);
[m1 m2] = size(month);
[d1 d2] = size(day);
if (mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0
is_leap_year = 1;
else
is_leap_year = 0;
end
if ~(y1==y2==d1==d2==m1==m2==1)
valid = false;
elseif any(month == [1, 3, 5, 7, 8, 10, 12])
valid = (day >0 && day <= 31)
elseif any(month == [4, 6, 9, 11])
valid = day >0 && day <= 30;
elseif month == 2 && is_leap_year == 1
valid = (day >0 && day <=29);
elseif month == 2 && is_leap_year == 0
valid = (day >0 && day <=28);
else
valid = false;
end
end
The result after submitting my program, all tests are passed except the one related to scalar values:
Why did my program fail on the non-scalar test?
The way you're checking for scalars is really not well defined.
~(y1==y2==d1==d2==m1==m2==1)
This chained equivalence check is not the same as checking if all of your variables are equal to 1, consider the following counter-example:
1==0==0==1 % Output: 1
In this case none of your comparison variables should be 0, so you might skirt this issue, but it's still best to avoid it. You're also contending with floating point comparisons which are prone to issues.
You should use isscalar, you say you tried it but didn't show how. Something like this should work:
function valid = valid_date(year, month, day)
if ~( isscalar(year) && isscalar(month) && isscalar(day) )
valid = false;
return
end
% ... other code now we know inputs are scalar
end
Thank you so much #Wolfie. The problem is solved.
I used what you told me about and put it at the beginning of my function.
I show you guys the code, in case you had the same error:
function valid = valid_date(year, month, day)
if ~(isscalar(year) && isscalar(month) && isscalar(day))
valid = false;
return
end
if ((mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0)
is_leap_year = 1;
else
is_leap_year = 0;
end
if any(month == [1, 3, 5, 7, 8, 10, 12])
valid = (day >0 && day <= 31);
elseif any(month == [4, 6, 9, 11])
valid = day >0 && day <= 30;
elseif (month == 2 && is_leap_year == 1)
valid = (day >0 && day <=29);
elseif (month == 2 && is_leap_year == 0)
valid = (day >0 && day <=28);
else
valid = false;
end
end
Yaaay all the tests are passed lhamdullah!
Hello everyone I'm trying to make a loop file to read several files, this is what I have done so far:
anoini = 1980;
anofin = 1981;
mesini = 1;
mesfin = 1;
diai=1;
diaf=1;
nano = (anofin-anoini)+1;
if (mesini == 1) || (mesini == 3) || (mesini == 5) || (mesini == 7) || (mesini == 8) || (mesini == 10) || (mesini == 12)
lmes = 31;
elseif (mesini == 4) || (mesini == 6) || (mesini == 9) || (mesini == 11)
lmes = 30;
elseif (mesini == 2)
lmes = 28;
end
for idia=1:lmes
for iano = anoini:anofin
for nn = 1:nano
D_1{nn,idia} = load(sprintf('F:\\salidas_nam\\%d\\%d%0.2u%0.2u06_NAM_day01.dat',iano,iano,mesini,idia));
end
end
end
For example I want this two files, but it only seems to read one
"F:\salidas_nam\1980\1980010106_NAM_day01.dat"
"F:\salidas_nam\1981\1981010106_NAM_day01.dat"
The idea is to automatize this so I can pick which years to read, it is not necesary to use sprintf, if someone knows how to do this in another way I will apreciate it.
I think the problem is in your inner loop. You were iterating over nn but not using it in the construction of the filename. So you ended up loading each file twice:
Therefore your output looked like this (repeated files):
D_1{1,1} = F:\salidas_nam\1980\1980010106_NAM_day01.dat
D_1{2,1} = F:\salidas_nam\1980\1980010106_NAM_day01.dat
D_1{1,2} = F:\salidas_nam\1981\1981010106_NAM_day01.dat
D_1{2,2} = F:\salidas_nam\1981\1981010106_NAM_day01.dat
...
However, I think you want this (no repeats):
D_1{1,1} = F:\salidas_nam\1980\1980010106_NAM_day01.dat
D_1{2,1} = F:\salidas_nam\1981\1981010106_NAM_day01.dat
D_1{1,2} = F:\salidas_nam\1980\1980010206_NAM_day01.dat
D_1{2,2} = F:\salidas_nam\1981\1981010206_NAM_day01.dat
...
If that is the case then you can drop the inner for loop and do something like this:
for idia=1:lmes
nn = 1;
for iano = anoini:anofin
D_1{nn,idia} = load(sprintf('F:\\salidas_nam\\%d\\%d%0.2u%0.2u06_NAM_day01.dat',iano,iano,mesini,idia));
nn = nn+1;
end
end
This does not address the potential leapyear issues you might have as Adriaan mentioned in the comments.
I write an algorithm that checks the stability of the closed system of the Nyquist criterion (http://en.wikipedia.org/wiki/Nyquist_stability_criterion)
function answear=stability(re,im)
%% Function check stability of system
%re is real part of transmitation
%im is imagine part of transmitation
%% Check number of vectors elements
re(end +1:5) = 0;
im(end +1:5) = 0;
if( length(re) > length(im))
root = length(re);
else
root = length(im);
end
for w=1:root
tran(w) = re(1) + re(2)*w.^1 + re(3)*w.^2 + re(4)*w.^3 + re(5)*w.^4 +1i*(...
im(1) + im(2)*w.^1 + im(3)*w.^2 + im(4)*w.^3 +im(5)*w.^4);
end
%% Algorithm
switch root
case 0
exist('Write nonzero numbers', 'var')
case 1
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
end
case 2
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
end
end
case 3
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
answear=1;
else
answear=0;
end
end
end
end
case 4
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
if( real(tran(w)) > 0 && imag(tran(w)) < 0)
answear=1;
else
answear=0;
end
end
end
end
end
end
%% Answear
if answear==1
disp('System unstable')
else
disp('System stable')
end
plot(real(tran),imag(tran))
grid on
end
Function returns
Undefined function or variable "answear".
Error in stability (line 87) if answear==1
So the algorithm is badly written?
Your code could use a lot of cleanup:
Instead of an if-statement such as this one:
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
you could write a boolean assignment:
answear = real(tran(w)) > 0 & imag(tran(w)) > 0);
Why do you have three (almost) identical nested if-statements at all?
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
First of all, you could replace everything with one if-statement. But what are you actually testing with this? It seems that those nested if statements are never executed. For instance, real(tran(w)) cannot be both positive and negative at the same time (unless it is a vector you are working on, in which case you shouldn't be using the operator &&).
Also, this is probably your code triggers the error regarding variable answear. Accessing it is impossible since it hasn't been assigned a value (none of the if-statements have been executed).
What is tran, and what is w? Are they global variables? If they are, pass them as input parameters. Your function is probably poorly designed if it relies on external states and variables.
I haven't actually run your code, but these suggestions should make it easier for you to debug it.
P.S:
Please fix the annoying spelling error (it is "answer" and not "answear") :)
Normally this function should give me the values 1, 2, 3 or 4. but when I use it, I get 0, 1 or 2. Could you help me to know where is the problem:
function Vecteur_retour = var_Test(Test)
AA = Test;
var_Test = zeros(1,2000);
for i=3:1:2000
if AA(i)<=AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=1;
else
if AA(i)<=AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=2;
if AA(i)>AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=3;
else
if AA(i)>AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=4;
end
end
end
end
end
Vecteur_retour = var_Test;
Vector comparisons will be much faster:
var_Test = ones(1,2000);
delta_Test = diff(Test);
var_Test([0 0 delta_Test(1:end-1)] > 0) = 2;
var_Test([0 delta_Test] > 0) = var_Test([0 delta_Test] > 0) + 2;
var_Test(1:2) = 0;
Probably because you never reach the cases var_Test(i) = 3 or var_Test(i) = 4.
You have a problem with your if and end blocks. The way you have it, case 3 is only reached if case 2 is hit first, but these are contradictory.
You want code more like.
function Vecteur_retour = var_Test(Test)
AA = Test;
var_Test = zeros(1,2000);
for i=3:1:2000
if AA(i)<=AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=1;
else
if AA(i)<=AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=2;
else % you forgot this else
if AA(i)>AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=3;
else
if AA(i)>AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=4;
end
end
end
end
end
Vecteur_retour = var_Test;
Careful indentation would have helped here.
I have this strange problem. I have a simple search requirements where a user can search a given entitiy (Say customer) based on several search criterias. User may choose to use a criteria or not. The search conditions need to 'AND' all the criteria. So I write code like this (which works)
IQueryable _customer;
_customer = from c in DS.properties
where
(txtCustomerName.Text.Length == 0 || c.name == txtCustomerName.Text)
&& (txtpropcust1.Text.Length == 0 || c.customfield1 == txtpropcust1.Text)
&& (txtpropcust2.Text.Length == 0 || c.customfield2 == txtpropcust2.Text)
&& (txtpropcust3.Text.Length == 0 || c.customfield3 == txtpropcust3.Text)
&& (txtpropcust4.Text.Length == 0 || c.customfield4 == txtpropcust4.Text)
&& (txtpropcust13.Text.Length == 0 || c.customfield13 == txtpropcust13.Text)
select c;
GridView1.DataContext = _customer;
The problem is that if I have 14 where clauses, the EF throws an error- 13 works - 14 does not.
I am using EF+WCF data service in a WPF application. Is there a setting somewhere which limits the number of where clauses?
Thanks
To simplify the resulting query, you could use:
var customers = DS.properties;
if (txtCustomerName.Text.Length > 0)
customers = customers.Where(x => x.name == txtCustomerName.Text);
if (txtpropcust1.Text.Length > 0)
customers = customers.Where(x => x.customfield1 == txtpropcust1.Text);
// etc
_customer = customers;
GridView1.DataContext = _customer;
Note that this will only add SQL where clauses when there's a need for it.