Dont why i keep getting reshape error anytime i run this.

6 views (last 30 days)
picture =imread('photographerFEP.png');
%variables
g = ['M' 'L' 'K' 'J' 'I' 'H' 'G' 'F' 'E' 'D' 'C' 'B' 'A' 'Z' 'Y' 'X' 'W' 'V' 'U' 'T' 'S' 'R' 'Q' 'P' 'O' 'N' ' ' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'];
o = (1:6:length(g)*6);
d = ('WE SHALL FIGHT ON THE BEACH IN THE DAYS OF NAPOLEON THE SAME WIND WHICH WOULD HAVE CARRIED HIS TRANSPORTS ACROSS THE CHANNEL MIGHT HAVE DRIVEN AWAY THE BLOCKADING FLEET THERE WAS ALWAYS THE CHANCE AND IT IS THAT CHANCE WHICH HAS EXCITED AND BEFOOLED THE IMAGINATIONS OF MANY CONTINENTAL TYRANTS MANY ARE THE TALES THAT ARE TOLD WE ARE ASSURED THAT NOVEL METHODS WILL BE ADOPTED AND WHEN WE SEE THE ORIGINALITY OF MALICE THE INGENUITY OF AGGRESSION WHICH OUR ENEMY DISPLAYS WE MAY CERTAINLY PREPARE OURSELVES FOR EVERY KIND OF NOVEL STRATAGEM AND EVERY KIND OF BRUTAL AND TREACHEROUS MANEUVER I THINK THAT NO IDEA IS SO OUTLANDISH THAT IT SHOULD NOT BE CONSIDERED AND VIEWED WITH A SEARCHING BUT AT THE SAME TIME I HOPE WITH A STEADY EYE WE MUST NEVER FORGET THE SOLID ASSURANCES OF SEA POWER AND THOSE WHICH BELONG TO AIR POWER IF IT CAN BE LOCALLY EXERCISED I HAVE MYSELF FULL CONFIDENCE THAT IF ALL DO THEIR DUTY IF NOTHING IS NEGLECTED AND IF THE BEST ARRANGEMENTS ARE MADE AS THEY ARE BEING MADE WE SHALL PROVE OURSELVES ONCE AGAIN ABLE TO DEFEND OUR ISLAND HOME TO RIDE OUT THE STORM OF WAR AND TO OUTLIVE THE MENACE OF TYRANNY IF NECESSARY FOR YEARS IF NECESSARY ALONE AT ANY RATE THAT IS WHAT WE ARE GOING TO TRY TO DO THAT IS THE RESOLVE OF HIS MAJESTYS GOVERNMENT-EVERY MAN OF THEM THAT IS THE WILL OF PARLIAMENT AND THE NATION THE BRITISH EMPIRE AND THE FRENCH REPUBLIC LINKED TOGETHER IN THEIR CAUSE AND IN THEIR NEED WILL DEFEND TO THE DEATH THEIR NATIVE SOIL AIDING EACH OTHER LIKE GOOD COMRADES TO THE UTMOST OF THEIR STRENGTH EVEN THOUGH LARGE TRACTS OF EUROPE AND MANY OLD AND FAMOUS STATES HAVE FALLEN OR MAY FALL INTO THE GRIP OF THE GESTAPO AND ALL THE ODIOUS APPARATUS OF NAZI RULE WE SHALL NOT FLAG OR FAIL WE SHALL GO ON TO THE END WE SHALL FIGHT IN FRANCE WE SHALL FIGHT ON THE SEAS AND OCEANS WE SHALL FIGHT WITH GROWING CONFIDENCE AND GROWING STRENGTH IN THE AIR WE SHALL DEFEND OUR ISLAND WHATEVER THE COST MAY BE WE SHALL FIGHT ON THE BEACHES WE SHALL FIGHT ON THE LANDING GROUNDS WE SHALL FIGHT IN THE FIELDS AND IN THE STREETS WE SHALL FIGHT IN THE HILLS; WE SHALL NEVER SURRENDER AND EVEN IF WHICH I DO NOT FOR A MOMENT BELIEVE THIS ISLAND OR A LARGE PART OF IT WERE SUBJUGATED AND STARVING THEN OUR EMPIRE BEYOND THE SEAS ARMED AND GUARDED BY THE BRITISH FLEET WOULD CARRY ON THE STRUGGLE UNTIL IN GODS GOOD TIME THE NEW WORLD WITH ALL ITS POWER AND MIGHT STEPS FORTH TO THE RESCUE AND THE LIBERATION OF THE OLD DELIVERED AT THE HOUSE OF COMMONS ON 4 JUNE 1940 CHURCHILL');
s = char(d);
for i = 1:length(s)
for j = 1:length(g)
if s(i) == g(j)
encrypt(i) = o(j); %#ok<SAGROW>
end
end
end
%display
disp('Your message encrypted')
disp(d)%original message
disp(' Cypher A')
disp(g)%Cypher A
imshow(picture)
title('Original Image')
disp('Numerical Message')
%Reshape
reshape = reshape(encrypt,50,50); %50x50
disp(reshape)%show numerical message
picture(51:100,51:100)=reshape;
figure
imshow(picture,[0,255])
title('Encoded Image')
%Decode
for i = 1:length(s)
for j = 1:length(g)
if y(i) == o(j)
org(i)= s(i);
end
end
end
disp('original messege')
disp(org)
This is my code and i cant figure out why i keep getting reshape error

Answers (2)

Geoff Hayes
Geoff Hayes on 22 Apr 2020
Aaron - when I try to run your code, I get the following error
Error: File: xyz.m Line: 25 Column: 1
"reshape" previously appeared to be used as a function or command, conflicting with its use here as the
name of a variable.
A possible cause of this error is that you forgot to initialize the variable, or you have initialized it
implicitly using load or eval.
This is because of the code found here
%Reshape
reshape = reshape(encrypt,50,50); %50x50
disp(reshape)%show numerical message
picture(51:100,51:100)=reshape;
where the variable reshape is the output of the reshape function - so a variable has been given the same name as the built-in MATLAB function. Never name a variable to be that of a function. Try this instead
reshapedData = reshape(encrypt,50,50); %50x50
disp(reshapedData)%show numerical message
picture(51:100,51:100)=reshapedData;
This will allow the code to execute but you will still have a problem with this error
Error using reshape
To RESHAPE the number of elements must not change.
Error in xyz (line 25)
reshapedData = reshape(encrypt,50,50); %50x50
because encrypt is a 1x2502 array which is two more elements more than the 2500 in the expected 50x50 reshapedData. You will need to determine how encrypt should be built so that it has 2500 elements only.
  2 Comments
Aaron Owusu-Teng
Aaron Owusu-Teng on 22 Apr 2020
Edited: Aaron Owusu-Teng on 22 Apr 2020
I tried these edits and I still get the reshape error. and I am looking at( Error using reshape
To RESHAPE the number of elements must not change.)
Geoff Hayes
Geoff Hayes on 22 Apr 2020
Yes, but encrypt is a 1x2502 array and so CANNOT be reshaped into a 50x50 array. You need to change the message string d to be a 1x2500 length message.

Sign in to comment.


James Tursa
James Tursa on 22 Apr 2020
This line
reshape = reshape(encrypt,50,50); %50x50
shadows the MATLAB reshape( ) function with your own variable called reshape. Don't do that. Now everytime you try to call the reshape( ) function MATLAB thinks you are trying to index into your local variable named reshape. Don't use variable names that are the same as MATLAB functions. Pick different names.

Categories

Find more on External Language Interfaces in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!