Click on row in TextArea?

조회 수: 5 (최근 30일)
Kurt
Kurt 2023년 1월 4일
댓글: Walter Roberson 2023년 1월 5일
I have a GUI that contains a TextArea. I read in a file that is 20,000 x 16 and convert each row to a string, so the resulting table is 20,000 x 1. Then I read that new "fits_text" table into the TextArea and use a formatSpec string to display it in neat columns, using tabs.
fits.TextArea.Value = fits_text;
So far, so good. Is there a way to click on a row of the text and determine which row it was, so I can process the data at that row in my original table (which is 20000 x 16)? Or do I need to use a UITable instead? I'm not sure I can fit a 16-column UITable into this GUI window.
Bonus question: It takes me about 7 minutes to re-format all 20,000 rows for display, using a "for" loop. I am pre-allocating space for the table. There must be a faster way. I can do this in Python in just a few seconds (but it doesn't look nearly as nice).
for i = 1:height(box_data)
fits_text{i,1} = sprintf(formatSpec, string(box_data{i,:}));
end

채택된 답변

Kurt
Kurt 2023년 1월 5일
The "compose" approach reduced the formatting from about 7 minutess to 10 seconds. Thanks!
I solved the TextArea size issue by leaving the 16-column TexArea alone, and creating a separate UITable off to the side containing just the first column of the TextArea - in effect, a "spinbox" containing text, not numbers. I can scroll through it and pick the appropriate line by clicking.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 1월 4일
편집: Walter Roberson 2023년 1월 4일
Unfortunately, TextArea do not have any selection callbacks; https://www.mathworks.com/help/matlab/ref/matlab.ui.control.textarea-properties.html
For the performance issue: use https://www.mathworks.com/help/matlab/ref/table.convertvars.html to build a new table from converting the existing variables to string. Then if you
fmt = [repmat('%s ', 1, 15), '%s'];
fits_text = compose(string(fmt), Table_Of_Strings{:,:});
This should, in theory, be higher performance.
If you wanted more careful control over the conversion of items into string then
fits_text = compose("Detailed % formats", box_data{:,1}, box_data{:,2}, box_data{:,3} ... box_data{:,16})
an uglier command line to be sure, but it gives per-item control like %6.2f and %-10s
  댓글 수: 2
Kurt
Kurt 2023년 1월 5일
Thanks. I'll try that.
Back to the performance issue: Is there a method to apply a full-blown function to an entire column without looping? I run into this a lot. I've done the simple one-liner, inline functions on a whole column, but I need something more. For example, the code to convert azimuth to right ascension is about 50 lines long. Is this possible?
Walter Roberson
Walter Roberson 2023년 1월 5일
arrayfun.... but it is just a hidden loop, and you are better off vectorizing the code, possibly using logical indexing.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by