Add new row to String Array.

조회 수: 26 (최근 30일)
Adam Brabec
Adam Brabec 2020년 7월 12일
댓글: Adam Brabec 2020년 7월 19일
Hi,
I have a Xx1 string array and I currently have something on the lines of:
strarray.full = [strarray.full;NEWDATA];
The code will add a new line each loop, and will re create the string array each time.
This works fine for smaller files, but the txt files I am reading are around 200,000 lines, so this takes around 20 minutes to run.
I am trying to get something like this to work to avoid having to re create the matrix each loop and make it alot faster.
strarray.full(end+1,1) = NEWDATA
I keep getting the Error "Unable to perform assignment becuse the indices on the left side are not compatible with the size of the right side"
The first loop results in an empty NEWDATA. I also do not believe I am allowed to share the code itself.
Thanks.

채택된 답변

the cyclist
the cyclist 2020년 7월 12일
You can preallocate memory for the entire string array with
str = strings(200000,1);
and then fill row-by-row rather than appending. This will avoid the string array being copied to different memory locations as it fills, and should radically speed up the operation.
  댓글 수: 1
Adam Brabec
Adam Brabec 2020년 7월 12일
편집: Adam Brabec 2020년 7월 12일
Hi Cyclist,
I was able to rework the code in the way you suggested so thank you.I'm not sure why, but it appears to be slower than the original method.
The loop times still drastically increase as the matrix is being filled. I stopped it at around 30 minutes.

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

추가 답변 (1개)

Adam Brabec
Adam Brabec 2020년 7월 13일
I was able to find a fix using a "Dumping matrix" that after each .txt file I read, it takes the dump matrix and add it to the end larger matrix.
This knocked the time down from 20 minutes to 7 minutes.
  댓글 수: 6
the cyclist
the cyclist 2020년 7월 18일
Not frustrated, and I don't feel like I wasted my time. I'm just trying to help you get to a solution efficiently.
Just bear in mind that any work you leave for someone else, rather than doing that prep work yourself, has to be done by every person who tries to help you. So, it can be a hurdle that some people just won't bother with.
Adam Brabec
Adam Brabec 2020년 7월 19일
Looking back at what I posted, I completely understand. From now I will be sure to do more prep work.
Thanks

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by