Is it possible to incorporate a hyperlink within a string array that references a specific variable while in a for loop

19 views (last 30 days)
Hello, I currently am writing a code that deals with the use of hyperlinks in MATLAB. I currently have a for loop that will set up a string array description. I would like this string array description to have a hyperlink in it that is associated with a specific variable that gets referenced in the for loop. In other words, when the hyperlink is clicked on, I would like the specific variable that was correlated with the specific for loop "i" value to be input into a function. The code below shows my attempt. As you can see I have a "names" vector with 3 names, and I would like the "descrip" array to have a hyperlink that allows me to run function "infoget" using a specific name (depending on the for loop iteration). I was hoping that the "descrip" array would store so that if I wanted the information associated with bob that would be in the "descrip{:,1}" position. So it becomes just a matter of calling that out, and clicking on the link.
names = ['bob' 'james' 'jim'];
for i = 1:length(names)
descrip{:,i} = sprintf('%s<br>%s</br><br>',...
'The link below will bring up the following information for a specific name',...
'<a href="matlab: infoget(%s) ">Information</a>',names{i});
end

Answers (2)

the cyclist
the cyclist on 17 May 2018
Your code will at least execute if you change
names = ['bob' 'james' 'jim']
to
names = {'bob' 'james' 'jim'}
Is that enough for you to see your way clear for any other corrections?
  1 Comment
Christopher Martinez
Christopher Martinez on 17 May 2018
Edited: Christopher Martinez on 17 May 2018
Oh my apologies I needed that to be fixed, but when I click on the hyperlink, it seems to not correlate the %s inside of the function with the names{i}. It instead places the name 'bob' within the description array itself and appears as:
{'The link below will bring up the following information for a specific name<br>Information</br><br>bob<br>'}
In addition I am getting the following error:
infoget(%s)
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

Sign in to comment.


the cyclist
the cyclist on 17 May 2018
Edited: the cyclist on 17 May 2018
I haven't figured out what you were trying to do with the breaks, but the core functionality works if you use this:
descrip{:,i} = sprintf('The link below will bring up the following information for a specific name: <a href="matlab: infoget(%s) ">Information</a>',names{i})
The main problem with your version (as I understand it), is that you were trying to put a formatSpec into the second argument of the sprintf. That won't work (unless maybe you nest another sprintf command, but I don't think you need to do that).

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!