unable to save values to xls read while taking values from xls write

1 view (last 30 days)
Hi everyone , i have a function where i have two loops
function key_out=updateKey(key, i) % Encryption
key_out=readmatrix('oneofmytry.xlsx'); %it has four random binary strings of 128 bit each as column vectors
key_out=(key_out)';
key_out=sprintf('%g', key_out);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:4
% some steps to change initails 128 bits inti new 128 bits one by one(1X128)
for i=1:10
%taking 128 bits from the previous loop and make 10 strings of 128 bits each (10X128)
%000000010101001......
%1000111...
%%%% I want to save these 10 new strings in one variable "key_out"
end
jj=key_out;
C(i,:,j)=jj();
C=C';
end
% All i want is to save to save key_out value as first 10 rows or columns and,
% and next ten values as next ten till my "j" reaches its limit
%i want to save 10 values at a time using writematrix, saving each value indvidually takes alot of time.
writematrix(C,'okay_try.xlsx');
end
  2 Comments
Walter Roberson
Walter Roberson on 10 Dec 2020
key_out is 10 x 128?
C(i,:,j)=jj();
If jj is not a scalar or vector, then you would be trying to assign a 2D array into a column vector.
Notice that your assignment to C uses the value of i even though for i is finished. Do you know what value i will have after the for i is done?
Maria Imdad
Maria Imdad on 10 Dec 2020
C(i,:,j)=jj();
si not working i am getting an error message as
"Unable to perform assignment because the size of the left side is 1-by-16 and the size of the right side is 1-by-128."

Sign in to comment.

Answers (1)

Raunak Gupta
Raunak Gupta on 17 Dec 2020
Hi,
As Walter mentioned, the assignment of C(i,:,j) = key_out is not possible as left side is a 1-D vector and right side is 2-D matrix which will give the mentioned error.
From the comments in the code I assume you need to store 4 chunk of 10 x 128 matrices into a xlsx file using writematrix. Since you want the chunk to be placed in a final 2-D matrix row-wise, in this case the size will become 40 x 128. You may find below example code useful for achieving the same.
function key_out=updateKey(key, i) % Encryption
key_out=readmatrix('oneofmytry.xlsx'); %it has four random binary strings of 128 bit each as column vectors
key_out=(key_out)';
key_out=sprintf('%g', key_out);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numOfSteps = 4;
numOfStrings = 10;
numOfBitsperString = 128;
% Intializing the final output C matrix
C = zeros(numOfSteps*numOfStrings,numOfBitsperString);
for j=1:numOfSteps
% some steps to change initails 128 bits inti new 128 bits one by one(1X128)
for i=1:numOfStrings
%taking 128 bits from the previous loop and make 10 strings of 128 bits each (10X128)
%000000010101001......
%1000111...
% I assume in this step we have 10x128 matrix as an answer in
% variable jj
end
C((j-1)*numOfStrings+1:j*numOfStrings,:) = jj;
end
% Here After loop is done for the number of Steps C will have a size of
% 40 x 128 where ten rows taken sequentially will represent one step.
writematrix(C,'okay_try.xlsx');
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!