How to remove one single quote at the beginning of a string?

18 visualizaciones (últimos 30 días)
Camille Godin
Camille Godin el 5 de Ag. de 2022
Editada: dpb el 5 de Ag. de 2022
So we're taking a string (which is the name of a file) and removing the .csv at the end of it to replace it by '.mat'. To do so, we simply concatenate the beginning of the first string to the '.mat'. The final result is the name of the file in a .mat format but with a simple problem: there is still a single quote at the beginning of it, and not at the end (and there shouldn't be any for the file to load properly). Example: '2021_06_24__09_54_01_381.mat
I want to know how to remove my single quote at the beginning of the string so I get 2021_06_24__09_54_01_381.mat
Maybe the solution requires a different approach entirely. Thank you so much in advance.
Here is my code (the meta variable contains all the file names I need to extract):
filename3 = strsplit(string(meta{i,1}),'.')
filename4 = strcat(filename3(1), '.mat');

Respuestas (1)

dpb
dpb el 5 de Ag. de 2022
Editada: dpb el 5 de Ag. de 2022
The MATLAB function to retrieve the extension and filename is fileparts and to rebuild a new one is fullfile
But, in this case you can shortcut the operation by
filenames=strrep(meta,'csv','mat');
which will leave you with the original cellstr() array -- if you really want a string array, wrap it around the above.
As for the original Q? itself about the extra single quote, that must be in the initial data, there's nothing in the above code that would introduce such an artifact. So, I'd recomend going back and figuring out where it comes from and fix the problem there.
If there's some external part of the process that is doing it and you can't for some reason fix it, then
filenames=strrep(filenames,"'",'');
will fix up the present problem, but it would still be better to fix it before/where it happens instead.
The introduction of the string() class, while in a lot of ways redundant functionality of the cellstr() with not all that much advantage has the side benefit of being able to write the above to wrap the single quote inside a string instead of having to double-up the ticks to escape them in char() strings that gets to be very difficult to get right/read.
  1 comentario
Camille Godin
Camille Godin el 5 de Ag. de 2022
Hi there, thank you for your answer. It worked!
It is the easiest fix, rather than changing how the file names are created in another program.

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by