Can I take a random variable and put in into imread so I can have it pull out different files from the same function?

I'm not really good at explaining things but basically I'm making a gui for a school project and I need to have cards appear when someone clicks a pushbutton. I have a deck of cards named 1.jpg, 2.jpg..... all the way to 52 .jpg. I'm currently using x=randperm(52,1) to get a specific card and simulate a deck. Right now my code is extremely long because I have to use an individual case for each instance of x that can occur (its over 800 lines at this point). So I wanted to know if it's even possible to put what x is into a single imread that will take the value of x and get the corresponding card file. IE if randperm pulls out a 37 I can get imread to pull out 37.jpg without having a specific imread for it. As I said I'm not even sure if it's possible in matlab, but any help would be appreciated.

 Accepted Answer

Sure thing
x=randperm(52,1)
inpict=imread(sprintf('%d.jpg',x))

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!