Can someone help me with the update function in Matlab to move values to MySQL? - matlab

I'm dealing with a problem with the update function in Matlab.
conn=database('MySQL','user','password');)
selectquery_select = 'SELECT * FROM inputs WHERE i_read = 0';
data_select = select(conn,selectquery_select);
for j=1:size(data_select)
id_data = data_select(j,1);
id_data = string(id_data.(1));
time_data = data_select(j,4);
time_data = string(time_data.(1));
time_dataform = datetime(time_data,'InputFormat','yyyy-MM-dd HH:mm:ss');
y0=data_select(j,2);
y0 = str2num(string(y0.(1)));
r0=data_select(j,3);
r0 = str2num(string(r0.(1)));
if id_data == "115"
run("C:\Users\...\uu.m")
update(conn,'inputs','i_read',1,'WHERE (ID_code = "115") AND WHERE (i_Time = time_data)');
end
end
Basically, I'm taking some value from the database when i_read is equal to 0 (i_read is a boolean variable in the database that should give 1 if the value is already processed and 0 if not). After a value is read, we want to change the i_read in the database from 0 to 1. We decide to use the update function, but this gave us the following error:
Error using database.odbc.connection/update
Too many input arguments.
Error in Patient_Identification (line 57)
update(conn,'inputs','i_read',1,'WHERE (ID_code = "112") AND WHERE (i_Time = ', time_data,')');
Someone is able to help us with this problem? Thank you.

Related

Variable not defined

I wanted to run this line but unfortunately it throws an error. Any ideas?
Undefined function or variable mr.
Error in Playground (line 23)
X = min(mr);
j = 1;
for i = 1:(resolution1+1)
line(i) = m(a(1))*ab(i)+c;
end
for i = 1:(resolution1)
if or(or(line(i)>ab_y(i) & line(i+1)<ab_y1(i+1),line(i)<ab_y1(i)& line(i+1)>ab_y1(i+1)),line(i)==ab_y1(i))
mr(j) = ab1(i);
rk(j) = ab_y1(j);
j = j+1;
end
end
X = min(mr);
Y = max(mr);
Your condition:
or(or(line(i)>ab_y(i) & line(i+1)<ab_y1(i+1),line(i)<ab_y1(i)& line(i+1)>ab_y1(i+1)),line(i)==ab_y1(i))
returns always false, so the statements inside
mr(j)=ab1(i); rk(j)=ab_y1(j);
are never executed. Therefore variable mr does not exist.
Add an mr = []; statement before the loop to initialize the variable (and also revise your condition why it returns always false).

How do I fix this error: "Undefined function or variable 'NaN'"?

I'm getting an error message in the yellow highlighted region that says "Undefined function or variable 'NaN'".
The purpose of this code is to determine the amount of change to give back to a customer. This is based on how much an item costs and how much was paid. Also, the code should return a flag saying if the transaction was completed.
This isn't all of my code, but I didn't want to make this longer than necessary.
Can anyone tell me what's wrong?
function [Change, flag] = makeChange(Cost, Paid)
extra = Paid-Cost;
if extra > 0
Change = extra;
flag = true;
elseif extra == 0
Change = 0;
flag = true;
return
else
flag = false;
Change = NaN;
warning('That''s not enough to buy that item.');
return
end
I couldn't confirm the problem with Octave 3.8.1.
octave:1> makeChange.m
error: 'Paid' undefined near line 4 column 9
error: called from:
error: /Path/to/makeChange.m at line 4, column 7
octave:1> Cost = 5
Cost = 5
octave:2> Paid = 10
Paid = 10
octave:3> [change, completed] = makeChange(Cost, Paid)
change = 5
completed = 1
octave:4> Cost = 10
Cost = 10
octave:5> Paid = 5
Paid = 5
octave:6> [change, completed] = makeChange(Cost, Paid)
warning: That's not enough to buy that item.
change = NaN
completed = 0
I would recommend reformatting your code to the following:
function [Change, flag] = makeChange(Cost, Paid)
Change = Paid-Cost;
flag = true;
if Change < 0
flag = false;
Change = NaN;
warning('That''s not enough to buy that item.');
return
else
return
end
By refactoring, you save yourself some needless comparisons, assignments, and logic.

Extraction of data from DWT subband

I am attempting to extract data from a DWT subband. I am able to embed data correctly (I have followed it in the debugger),cal PSNR etc. PSNR rate seem very high 76.2?? however,I am having lot of trouble extracting data back!It is sometimes extracting the number 128?? Can anyone help or have any idea why this is? I would be very thankful.I have been working on this all day & having no luck!I am very curious to know??
Data Embedding:
coverImage = imread('lena.bmp');
message = importdata('minutiaTest.txt');
%message = 'Bifurcations:';
[LL,LH,HL,HH] = dwt2(coverImage,'haar');
if size(message) > size(coverImage,1) * size(coverImage,2)
error ('message too big to embed');
end
bit_count = 0;
steg_coeffs = [4, 4.75, 5.5, 6.25, 7];
for jj=1:size(message,2)+1
if jj > size(message,2)
charbits = [0,0,0,0,0,0,0,0];
else
charbits = dec2bin(message(jj),8)';
charbits = charbits(:)'-'0';
end
for ii=1:8
bit_count = bit_count + 1;
if charbits(ii) == 1
if HH(bit_count) <= 0
HH(bit_count) = steg_coeffs(randi(numel(steg_coeffs)));
end
else
if HH(bit_count) >= 0
HH(bit_count) = -1 * steg_coeffs(randi(numel(steg_coeffs)));
end
end
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'newStego.bmp');
Data Extraction:
new_Stego = imread('newStego.bmp');
[LL,LH,HL,HH] = dwt2(new_Stego,'haar');
message = '';
msgbits = '';
for ii = 1:size(HH,1)*size(HH,2)
if HH(ii) > 0
msgbits = strcat (msgbits, '1');
elseif HH(ii) < 0
msgbits = strcat (msgbits, '0');
else
return;
end
if mod(ii,8) == 0
msgChar = bin2dec(msgbits);
if msgChar == 0
break;
end
msgChar = char (msgChar);
message = [message msgChar];
msgbits = '';
end
end
The problem arises from reading your data with importdata.
This command will load the data to an array. Since you have 39 lines and 2 columns (skipping any empty lines), its size will be 39 2. However, the program assumes that your message will be a string. For example, 'i am a string' has a size 1 13. This expectation of the program compared to the data you actually give it creates all sorts of problems.
What you want is to read your data as a single string, where the number 230 is not one element, but 3 individual characters. Tabs and newlines will also be read in as well.
To read your file:
message = fileread('minutiaTest.txt');
After you extract your message, to save it to a file:
fid = fopen('myFilename.txt','w');
fprintf(fid,message);
fclose(fid);

I got error message about simulink "Output argument is not assigned on some execution paths"

In simulink, I made some model using "MATLAB function"block
but I met error message here.
here is code and error message.
function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
if mode == 1
VTAS = initialVTAS + (a * t) ;
postVTAS = VTAS;
elseif mode == 2
datasize = length(preVTAS);
lastvalue = preVTAS(datasize);
VTAS = lastvalue + 0;
postVTAS = VTAS;
end
end
Output argument 'VTAS' is not assigned on some execution paths.
Function 'MATLAB Function' (#36.25.28), line 1, column 26:
"fcn"
Launch diagnostic report.
I think there is no problem about output "VTAS"
please teach me what is a problems.
As the compiler tells you, under some circumstances there is no output value assigned to VTAS. The reason is that you only assign values to that output if mode is 1 or 2. The compiler doesn't know what values are feasible for mode. To remedy this, you need to make sure that VTAS is assigned under any and all circumstances.
This could be accomplished by, e.g. adding an else construct, like so:
function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
if mode == 1
VTAS = initialVTAS + (a * t) ;
postVTAS = VTAS;
elseif mode == 2
datasize = length(preVTAS);
lastvalue = preVTAS(datasize);
VTAS = lastvalue + 0;
postVTAS = VTAS;
else
VTAS = NaN;
postVTAS = NaN;
end
end
Edit:
Additionally, it would be good practice for the else case to throw an error. This would be helpful for debugging.
As a minor note, for every case, postVTAS is equal to VTAS, so essentially it is superfluous to return both from the function.

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1