IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MATLAB Discussion :

probleme de fontion pour Convertir decimal negatif decimal to binaire


Sujet :

MATLAB

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 12
    Points : 8
    Points
    8
    Par défaut probleme de fontion pour Convertir decimal negatif decimal to binaire
    Bonjour,
    j'ai un souci dans ma fionction de matlab
    ce que je cherche de converti un vecteur x en binaire et ecrir les resultat dans un fichier text
    mon problem avec cette fonction avec les nobre negatif
    quelqu'un peux m'aider
    merci d'avance

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    function [] = printbinary(x,B,fname)
    %PRINTBINARY print values of vector x in binary form into file fname.txt
    % PRINTBINARY takes values of vector x,converts them to B bit two's complement
    % binary format, and then prints the binary representation to the file
    % "fname.txt".This i s used to print the coefficients of the
    % decimating polyphase filter (s) in the intermediate frequency to baseband
    % conversion process to afile so that they can be used to load the ROM
    % modules in the VHDL implementation.
    %
    % INPUT ARGUMENTS
    % - x - the vector that you desire to print in binary format
    % - B - the number of bits in the binary representation
    % - fname - a string representing the file where you want the data to
    % be written to
    fid=fopen([fname,'.txt' ],'wt'); % open a file in write mode
    x=dec2bin(x,B); % convert elements of vector into B-bit binary
    [row col]=size(x); % calculate number of rows and columns in binary ve c tor
    for i=1:row % loop through rows
    for j=1:col
    if (x(i,j )=='/') % if number is negative, convert'/'s to 1's
    x(i,j)='1';
    end
    fprintf(fid,'%c ',x(i,j)); % print elements of vector to a file,one character at a time
    end
    fprintf(fid ,'%c\n ',' '); % print a newline character to the file
    end
    fclose (fid); % close the file

  2. #2
    Expert confirmé
    Avatar de duf42
    Homme Profil pro
    Formateur en informatique
    Inscrit en
    Novembre 2007
    Messages
    3 111
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Formateur en informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 111
    Points : 4 661
    Points
    4 661
    Par défaut
    Bonjour,

    Je pense que la première question à te poser est de savoir comment tu veux représenter les entiers négatifs (Wikipédia: Représentation d'entiers négatifs)

    Duf

    EDIT: Au passage, ton code doit pouvoir se simplifier:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    x(x=='/')='1'; % if number is negative, convert'/'s to 1's
    Simulink & Embedded Coder

    Au boulot : Windows 7 , MATLAB r2016b
    A la maison : ArchLinux mais pas MATLAB

Discussions similaires

  1. Probléme pour convertir decimal en hexa
    Par vtk37 dans le forum Débuter
    Réponses: 7
    Dernier message: 19/05/2009, 14h00
  2. convertir un nombre decimal en heure
    Par missamissi dans le forum MATLAB
    Réponses: 7
    Dernier message: 06/05/2009, 13h43
  3. fonction pour convertir une valeur en decimal
    Par zaki_1982 dans le forum VBScript
    Réponses: 6
    Dernier message: 16/02/2009, 13h12
  4. Fonction pour convertir un fichier IDX en binaire ?
    Par Marie.B dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 17/12/2008, 14h08
  5. Réponses: 13
    Dernier message: 05/07/2006, 09h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo